nextSibling和lastSibling

來源:互聯網
上載者:User

標籤:blog   http   使用   os   io   ar   問題   cti   

在FireFox中包含眾多空格作為文本節點,因此在我們使用nextSibling和previousSibling時就會出現問題。因為FireFox會把文本節點誤當做元素節點的兄弟節點來處理。我們可以添加nodeType來判斷。當上一節點或者是下一節點為文本節點時,就繼續尋找,直到找到下一個元素節點。以下代碼僅供參考,在fireFox中測試通過:

        //下一個兄弟節點        function nextSibling(node) {            var tempLast = node.parentNode.lastChild;            if (node == tempLast) return null;            var tempObj = node.nextSibling;            while (tempObj.nodeType != 1 && tempObj.nextSibling != null) {                tempObj = tempObj.nextSibling;            }            return (tempObj.nodeType==1)? tempObj:null;        }        //前一個兄弟節點        function prevSibling(node) {            var tempFirst = node.parentNode.firstChild;            if (node == tempFirst) return null;            var tempObj = node.previousSibling;            while (tempObj.nodeType != 1 && tempObj.previousSibling != null) {                tempObj = tempObj.previousSibling;            }            return (tempObj.nodeType==1)? tempObj:null;        }    
測試代碼

其中nodeType的值主要有以下幾種:

  1. 元素節點的nodeType值為1
  2. 屬性節點的nodeType值為2
  3. 文本節點的nodeType值為3

聯繫我們

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