AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

18 個高級工程師必須會的強大JavaScript 技巧

來源: 責編: 時間:2023-08-14 22:01:28 365觀看
導讀瀏覽器01、實現全屏當您需要將當前屏幕顯示為全屏時function fullScreen() { const el = document.documentElement const rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.moz

瀏覽器

3Lc28資訊網——每日最新資訊28at.com

12、字符串腳本

當需要將一串字符串轉換為JavaScript腳本時,該方法存在xss漏洞,請謹慎使用3Lc28資訊網——每日最新資訊28at.com

const obj = eval('({ name: "jack" })')// obj will be converted to object{ name: "jack" }const v = eval('obj')// v will become the variable obj

13、遞歸函數名解耦

當需要編寫遞歸函數時,聲明了函數名,但是每次修改函數名時,總是忘記修改內部函數名。argument是函數的內部對象,包括傳入函數的所有參數,arguments.callee代表函數名。3Lc28資訊網——每日最新資訊28at.com

// 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 元素3Lc28資訊網——每日最新資訊28at.com

14、隱性判斷

當需要判斷某個dom元素當前是否出現在頁面視圖中時,可以嘗試使用IntersectionObserver來判斷。3Lc28資訊網——每日最新資訊28at.com

<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>

15、元素可編輯

當你需要編輯一個dom元素時,讓它像文本區域一樣點擊。3Lc28資訊網——每日最新資訊28at.com

<contenteditable="true">here can be edited</div>

16、元素屬性監控

<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>

17、打印 dom 元素

當開發過程中需要打印dom元素時,使用console.log往往只能打印出整個dom元素,而無法查看dom元素的內部屬性。你可以嘗試使用 console.dir3Lc28資訊網——每日最新資訊28at.com

console.dir(document.body)

其他3Lc28資訊網——每日最新資訊28at.com

18、激活應用程序

當你在移動端開發時,你需要打開其他應用程序。還可以通過location.href賦值來操作以下方法3Lc28資訊網——每日最新資訊28at.com

<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>

總結

以上就是我今天想與你分享的全部內容,希望對你有所幫助,最后,感謝你的閱讀,祝編程愉快!3Lc28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-5744-0.html18 個高級工程師必須會的強大JavaScript 技巧

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 新興技術趨勢將徹底改變我們的世界

下一篇: SpringCloud Gateway 路由如何定位從底層源碼分析

標簽:
  • 熱門焦點
Top