Determine if an element is node text using jQuery

Given a jQuery element, how do I determine if the sibling on the right is node text and not another element? In PHP, you would compare nodeType with #text - which is equivalent in this case?

 window.jQuery('body').find('a').each(function (i) { if(window.jQuery(this).next() == '?'){ } }); 

I am trying to figure out what I can add to the state.

Update

  if(window.jQuery(this).next().length != 0){ alert(window.jQuery(this).next().get(0).nodeType); if(window.jQuery(this).next().get(0).nodeType == 3){ alert('right has text'); } 

For some reason, all my tests keep returning 1, not 3, to indicate text nodes!

+6
source share
2 answers

next() returns only elements, so you cannot use it to move text nodes. Instead, you can use the nextSibling DOM nextSibling and check its nodeType property:

Live demo: http://jsfiddle.net/kD9qs/

the code:

 window.jQuery('body').find('a').each(function (i) { var nextNode = this.nextSibling; if (nextNode && nextNode.nodeType == 3) { alert("Next sibling is a text node with text " + nextNode.data); } }); 
+16
source

as a comment:

check out nodeType and see if it helps you.

+2
source

Source: https://habr.com/ru/post/898286/


All Articles