有人可以澄清爲什麼下面的代碼返回#text而不是'li' ?previousSibling和nextSibling返回文本?
第一個李的下一個兄弟姐妹不應該是李? 同樣以前的李的同胞是李李?
<body>
<ul>
<!-- comment -->
<li id="A"></li>
<li id="B"></li>
<!-- comment -->
</ul>
<script>
//cache selection of the ul
var ul = document.querySelector('ul');
//What is the nextSibling of the first li?
console.log(ul.querySelector('#A').nextSibling.nodeName); //logs text
//What is the previousSibling of the last li?
console.log(ul.querySelector('#B').previousSibling.nodeName); //logs text
</script>
</body>
嘗試'previousElementSibling'和'nextElementSibling'只是元素。 – elclanrs