3Lc28資訊網——每日最新資訊28at.com
當需要將一串字符串轉換為JavaScript腳本時,該方法存在xss漏洞,請謹慎使用
const obj = eval('({ name: "jack" })')// obj will be converted to object{ name: "jack" }const v = eval('obj')// v will become the variable obj
當需要編寫遞歸函數時,聲明了函數名,但是每次修改函數名時,總是忘記修改內部函數名。argument是函數的內部對象,包括傳入函數的所有參數,arguments.callee代表函數名。
// This is a basic Fibonacci sequencefunction fibonacci (n) { const fn = arguments.callee if (n <= 1) return 1 return fn(n - 1) + fn(n - 2)}
DOM 元素
當需要判斷某個dom元素當前是否出現在頁面視圖中時,可以嘗試使用IntersectionObserver來判斷。
<style>.item { height: 350px;}</style><class="container"> <class="item" data-id="1">Invisible</div> <class="item" data-id="2">Invisible</div> <class="item" data-id="3">Invisible</div></div><script> if (window?.IntersectionObserver) { let items = [...document.getElementsByClassName("item")]; // parses as a true array, also available Array.prototype.slice.call()let io = new IntersectionObserver( (entries) => { entries.forEach((item) => { item.target.innerHTML = item.intersectionRatio === 1 // The display ratio of the element, when it is 1, it is completely visible, and when it is 0, it is completely invisible ? `Element is fully visible` : `Element is partially invisible`; }); }, { root: null, rootMargin: "0px 0px", threshold: 1, // The threshold is set to 1, and the callback function is triggered only when the ratio reaches 1 } ); items.forEach((item) => io.observe(item)); }</script>
當你需要編輯一個dom元素時,讓它像文本區域一樣點擊。
<contenteditable="true">here can be edited</div>
<id="test">test</div><button onclick="handleClick()">OK</button><script> const el = document.getElementById("test"); let n = 1; const observe = new MutationObserver((mutations) => { console.log("attribute is changede", mutations); }) observe.observe(el, { attributes: true }); function handleClick() { el.setAttribute("style", "color: red"); el.setAttribute("data-name", n++); } setTimeout(() => { observe.disconnect(); // stop watch }, 5000);</script>
當開發過程中需要打印dom元素時,使用console.log往往只能打印出整個dom元素,而無法查看dom元素的內部屬性。你可以嘗試使用 console.dir
console.dir(document.body)
其他
當你在移動端開發時,你需要打開其他應用程序。還可以通過location.href賦值來操作以下方法
<a href="tel:12345678910">phone</a><a href="sms:12345678910,12345678911?body=hello">android message</a> <a href="sms:/open?addresses=12345678910,12345678911&body=hello">ios message</a><a href="wx://">ios message</a>
以上就是我今天想與你分享的全部內容,希望對你有所幫助,最后,感謝你的閱讀,祝編程愉快!
本文鏈接:http://www.tebozhan.com/showinfo-26-5744-0.html18 個高級工程師必須會的強大JavaScript 技巧
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 新興技術趨勢將徹底改變我們的世界