开发者

jQuery: getting next node (also text nodes)

I've a jQuery object and I shall retrieve the next sibling node, which may be a text node. For example:

<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 level 2 allows to get the next NODE sibling, but jQuery next() works on elements (nodeType 1, I guess). I am asking this because I'm already using jQuery and I prefer to don't touch the DOM nodes directly (also because jQuery itself may provide a layer of abstraction from DOM and may run where DOM level 2 is not supported, but this is only a thought).

If jQuery doesn't provide this, shall I use DOM like above, or there are better options?

EDIT: I forgot something: I don't want to get ONLY text elements, but any kind of node, just as nextSibling does. I'm using .contents() to iterate over the content, but this is pretty annoying (and slow, and many other bad things) when you j开发者_如何学Pythonust need the next node and DOM provides a solution.

EDIT2: Looking jQuery source code, it seems it relies on nextSibling.


Use the DOM. Don't be scared of it; 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 thing, using the DOM will in fact work in more browsers than the jQuery version.


To my knowledge, there is no method in jquery like nextSibling in javaScript which also returns text elements.But you can go to the parent element and use contents() as it will also consider text elements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜