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>

 

posted @ 2017-12-27 17:16  雨落知音  阅读(4761)  评论(0编辑  收藏  举报