Hide an element's next sibling with Javascript

前端 未结 4 1549
天命终不由人
天命终不由人 2020-12-09 04:23

I have an element grabbed from document.getElementById(\'the_id\'). How can I get its next sibling and hide it? I tried this but it didn\'t work:



        
4条回答
  •  孤城傲影
    2020-12-09 05:07

    Take a look at the Element Traversal API, that API moves between Element nodes only. This Allows the following:

    elem.nextElementSibling.style.display = 'none';
    

    And thus avoids the problem inherent in nextSibling of potentially getting non-Element nodes (e.g. TextNode holding whitespace)

提交回复
热议问题