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