📜  nextsibling vs nextelementsibling - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:43.798000             🧑  作者: Mango

代码示例1
// nextElementSibling always returns an element. 
// nextSibling can return any kind of node.
// They are the same for your example, but different in other

Here is span-01 Some text at the top level Here is span-02

document.getElementById('span-01').nextElementSibling //is span-02 document.getElementById('span-01').nextSibling // is the text node containing // "Some text at the top level" //note: //
Here is div-1
Here is div-2
var el = document.getElementById('div-1').nextSibling // is #text node var el2 = document.getElementById('div-2').nextSibling // is #text node //text nodes are inserted in the DOM where whitespace occurs between tags //(i.e. after the closing tag of an element and before the opening tag of the next). //source : https://developer.mozilla.org/en-US/docs/Web/API/Node/nextSibling#example