怎样获取当前节点前面临近的第一个节点

使用: Node.prototype.previousSbling

用法和 Node.prototype.nextSbling 相同.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="id1">李雷</div><div id="id2">韩梅梅</div>
    <script>
        document.getElementById('id2').previousSibling === document.getElementById('id1'); // true
        document.getElementById('id2').previousSibling.firstChild.nodeValue; // 李雷
    </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/aisowe/p/11506116.html
今日推荐