javascript uuid技術

來源:互聯網
上載者:User

這主要用於選取器過濾重複元素時用的。

涉及三種可能的手段:sourceIndex,uniqueID與自訂的uuid。

sourceIndex 擷取元素節點在頁面的出現順序,即為其在document.all集合中的索引號。由於要準確擷取先後位置,如果在其前面插入一個元素節點,那麼它的位置就要加一。換言之,它是動態分配的,每次都要重複計算。注意,IE only,唯讀。

uniqueID 也是一個IE only的唯讀屬性。這個屬性也不是早早就固化在元素節點中,只在我們第一次訪問它時才開始為整個頁面分配uniqueID。這意味著,如果我們用它來做選取器的過濾,第一次可能會很慢。另,document對象也具有這個屬性,不過它和其它HTML的uniqueID屬性的意義是完全不同的。當我們每調用一次document.uniqueID,系統就會返回一個新的uniqueID值。

   window.onload = function(){        alert(document.uniqueID)//注意:IE only        alert(document.uniqueID)        alert(document.uniqueID)    }

<br /><!doctype html><br /><html lang="en"><br /> <head><br /> <meta charset="utf-8" /><br /> <meta content="IE=8" http-equiv="X-UA-Compatible"/><br /> <title>uuid by 司徒正美 </title><br /> <script type="text/javascript"></p><p> window.onload = function(){<br /> alert(document.uniqueID)<br /> alert(document.uniqueID)<br /> alert(document.uniqueID)<br /> }<br /> </script><br /> </head><br /> <body></p><p> </body><br /></html><br />

運行代碼

這兩個東西都是IE ony,而且只能用於HTMLDocument,因此我們註定要自行設計一個通行的方案。看jQuery是怎樣實現的:

         var expando = "jQuery" + now(), uuid = 0,        //*****************略*******************        var id = elem[ expando ]

不過在IE下,有些元素在添加自訂屬性時會出錯,如object,applet,embed。這比較作孽。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.