javascript相鄰節點元素獲取


<script>
    window.onload = function () {
        var myLinkItem = document.getElementById('linkItem');
        var first = firstSibling(myLinkItem.parentNode);
        var last = lastSibling(myLinkItem.parentNode);
        alert(getTextContent(first));
        alert(getTextContent(last));
    }
    function lastSibling(node) {
        var tempObj = node.parentNode.lastChild;
        while (tempObj.nodeType != 1 && tempObj.previousSibling != null) {
            tempObj = tempObj.previousSibling;
        }
        return (tempObj.nodeType == 1) ? tempObj : false;
    }
    function firstSibling(node) {
        var tempObj = node.parentNode.firstChild;
        while (tempObj.nodeType != 1 && tempObj.nextSibling != null) {
            tempObj = tempObj.nextSibling;
        }
        return (tempObj.nodeType == 1) ? tempObj : false;
    }
    function getTextContent(node) {
        return node.firstChild.nodeType;
    }
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM