element.nextElementSibling与element.previousElementSibling案例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

</head>
<body>
<div>
    <ul>
        <li>123</li>
        <li>123</li>
        <li>123</li>
        <li id="four">第四个li</li>
        <li>123</li>
        <li>123</li>
        <li>123</li>
        <li>123</li>
        <li>123</li>
        <li>123</li>
    </ul>
</div>
<script>
    let li_4 = document.getElementById(‘four‘);
    li_4.previousElementSibling.style.backgroundColor = ‘pink‘;
    li_4.nextElementSibling.style.backgroundColor = ‘red‘;
</script>
</body>
</html>

相关推荐