標籤: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的值主要有以下幾種:
- 元素節點的nodeType值為1
- 屬性節點的nodeType值為2
- 文本節點的nodeType值為3