iPhone賣不動了!蘋果股價創年內最大日跌幅:市值一夜蒸發萬億元
8月5日消息,今天凌晨美股三大指數高開低走集體收跌,道指跌0.41%;納指跌0.36%;標普500指數跌0.52%。熱門科技股也都變化極大,其中蘋果報181.99美元,跌4.8%,創
日常開發中,我們經常會用到很多通用的 JS 代碼,比如:復制內容、從 URL 中獲取指定參數等,這些代碼通常有固定實現,即:代碼片段。所以,為了方便大家的開發,今天咱們就來看看常用的 7 種代碼片段。
通過按鈕,將指定 dom 中的內容復制到用戶的剪貼板
const copyToClipboard = (content) => { const textarea = document.createElement("textarea") textarea.value = content document.body.appendChild(textarea) textarea.select() document.execCommand("Copy") textarea.remove()}
這應該是一個非常常見的操作,之前經常會使用 正則來完成,現在有了更簡單的方式:
const getQueryByName = (name) => { const query = new URLSearchParams(location.search) return decodeURIComponent(query.get(name))}// url: https://sunday.com/?name=fatfish&age=100const name = getQueryByName('name') // fatfishconst age = getQueryByName('age') // 100const gender = getQueryByName('gender') // null
const scrollToTop = () => { const c = document.documentElement.scrollTop || document.body.scrollTop if (c > 0) { window.requestAnimationFrame(scrollToTop) window.scrollTo(0, c - c / 8) }}
const getScrollPosition = (el = window) => ({ x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft, y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop,})getScrollPosition() // { x: 0, y: 215 }
function getOSType() { let u = navigator.userAgent, app = navigator.appVersion let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1 let isIOS = !!u.match(//(i[^]+( U)? CPU.+Mac OS X/) if (isIOS) { return 0 } else if (isAndroid) { return 1 } else { return 2 }}getOSType() // 0
const formatMoney = (money) => { return money.toLocaleString()}formatMoney(123456789) // '123,456,789'formatMoney(123456789.123) // '123,456,789.123'formatMoney(123) // '123'
// 進入全屏function fullScreen() { let el = document.documentElement let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen //typeof rfs != "undefined" && rfs if (rfs) { rfs.call(el) } else if (typeof window.ActiveXObject !== "undefined") { let wscript = new ActiveXObject("WScript.Shell") if (wscript != null) { wscript.SendKeys("{F11}") } }}// 退出全屏function exitScreen() { let el = document let cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.mozCancelFullScreen || el.exitFullScreen //typeof cfs != "undefined" && cfs if (cfs) { cfs.call(el) } else if (typeof window.ActiveXObject !== "undefined") { let wscript = new ActiveXObject("WScript.Shell") if (wscript != null) { wscript.SendKeys("{F11}") } }}
本文鏈接:http://www.tebozhan.com/showinfo-26-91166-0.html簡化你的工作,七種常用的 JS 代碼片段
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com