Llamar a jQuery next () sin devolver el objeto nextSibling

My document contains a text node with a span node after it. In the Chrome inspector, the text node's nextSibling object is the span node. However, calling textnode.next() returns 0 objects. I'm not adding a selector to the next call and seemingly identical situations throughout the code all work as expected.

Here's what the relevant DOM tree looks like:

<span id="parent-node">
  some text
  <span id="sibling-node">the span's text</span>
  more text

Why would calling next() on the "some text" node not return the "sibling-node" span?

preguntado el 01 de febrero de 12 a las 04:02

Selecting a textnode playing around with it is always problematic. Why don't you select the span directly if it has an id? -

jQuery doesn't generally operate on text nodes. It works on elements. For example, next() skips right over text nodes and ignored them. -

1 Respuestas

Desde los docs...

Given a jQuery object that represents a set of Elementos DOM,la .next() method allows us to search through the immediately following sibling of these elements in the DOM tree and construct a new jQuery object from the matching elements.

So according to the docs, it is expected that .next() se llama desde un elementos node, not just any node.

Calling from a text node isn't supported.

Respondido 01 Feb 12, 09:02

I wasn't aware text elements weren't part of the DOM. Thanks for the clarification. Is there an efficient fix? I can obviously call parent() and search for the text node and then get the next element in the sibling list, but that seems unnecessarily time consuming. - Jack

@Jack: They are part of the DOM, but they're not DOM elements. An "element" is specifically a Type 1 DOM node, while a text node is a Type 3 node. The fix would probably just be to use the native nextSibling instead of a jQuery method. - user1106925

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.