您未必知道的Js技巧

來源:互聯網
上載者:User

1.       document.GetElementsByTagName可以獲得某一個標籤的所有元素,可以通過document.getElementsByTagName(‘*’)獲得頁面上的所有元素,這裡星號代表任何標籤,而注釋標籤的tagName是”!”

2.       String 的方法substr與substring的區別
substr(start[,length])表示從start位置開始取length個字串
substring(start,end)表示從start,到end之間的字串,包括start位置的字元但是不包括end位置的字元

3.       “==”與“===”
前者比較兩個對象會自動進行類型轉換例如比較’12’ == 12將返回true;而後者不進行任何轉換必須完全相等’12’ === 12將返回false

4.       如何使網頁不可以被儲存
<noscript><iframe src=*.html></iframe></noscript>

5.       使用document.referrer可以獲得上一頁的地址

6.       使用input元素的form屬性可以得到該input所屬的form元素

7.       Script標籤中的defer屬性
作用是告訴瀏覽器可以繼續執行html,而不必先執行src指定的js代碼,直到發生不能繼續執行的情況。

8.       通過arguments可以得到傳遞到當前執行function的所有參數的一個數組,即便你沒有給該function定義任何參數。

9.       可以使用document.anchors獲得頁面上面所有的連結元素,而不必使用document.getElementsByTagName(‘A’);同樣道理還有:document.forms,document.images

10.   使用js判斷用戶端機器是否安裝了.net framework
function hasDotNetFramework(){return navigator.userAgent.indexOf(".NET CLR") > -1;}

11.   在用戶端事件中使用&quot;代替“””
例如:var html = ‘<a href=”javascript:alert(&quot;this is message&quot;)”>hello</a>’

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.