JQuery: getting the next node (also text nodes)

I have a jQuery object, and I will get the next brother node, which may be the text node. For instance:

<div id="test">some text<br/>other text</div>

var test = $("#test");
var br = $("br", test);
alert(br.next().length); // No next ELEMENTS
alert(br.get(0).nextSibling.nodeValue); // "other text"
alert(br.get(0).nextSibling.nextSibling); // null

The DOM 2 level allows you to get the next NODE sibling, but jQuery next () works with elements (nodeType 1, I think). I ask this because I already use jQuery, and I prefer not to touch the DOM nodes directly (also because jQuery itself can provide an abstraction layer from the DOM and can run where the DOM 2 level is not supported, but that's just a thought) .

If jQuery does not provide this, should I use the DOM as above, or are there better options?

EDIT: I forgot something: I do not want to receive ONLY text elements, but any kind of node, as nextSibling does. I use .contents () to iterate over the contents, but it is rather annoying (and slowly and many other bad things) when you just need the following NODE and DOM that provide a solution.

EDIT2: Looking for jQuery source code, it looks like it relies on nextSibling.

+3
source share
1 answer

Use the DOM. Do not be afraid of him; it’s easy, and you already seem to know what to use. jQuery is built on top of the DOM, and for this kind of use, the DOM actually works in more browsers than the jQuery version.

+7
source

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


All Articles