Find Next Sibling Element In DOM With JavaScript

后端 未结 2 743
感情败类
感情败类 2020-12-20 18:47

How can I find the next element in the current scope? For example, I want to find div element and after this the next one which is p?

If I

相关标签:
2条回答
  • 2020-12-20 19:05

    You want .nextElementSibling.

    document.getElementsByTagName('DIV')[0].nextElementSibling
    

    The reason you are getting #text is because nextSibling will return the next node in the childNodes list of the parent element. In this case it is whitespace (hence #text) because of the newline used between </div> and <p>.

    0 讨论(0)
  • 2020-12-20 19:07

    You can use nextElementSibling. If you need to support older browsers, traverse nextSibling until you find an Element node.

    0 讨论(0)
提交回复
热议问题