简体   繁体   中英

How can I get the text between 2 elements

<div class="container">
  <a href="#" class="link">asd</a>
  [I want get this text]
  <a href="#" class="link">asd</a>
  Anouther text i dont need.
</div>

How can I withdraw the text between elements (not the inner text of elements, in this case I should get "[I want get this text]")?

DEMO

I think the simplest way would be to deal with the native dom elements:

var a = $(".container a")[0];
var textNode = a.nextSibling;
var text = textNode.textContent;

Note that var text = textNode.nodeValue; will also work, but I'm not sure which is preferable.

One way is to enclose the part in a span tag and get the contents of the span:

<div class="container">
      <a href="#" class="link">asd</a>
      <span id="i_want_this">[I want get this text]</span>
      <a href="#" class="link">asd</a>
      Anouther text i dont need.
</div>

myVal = $("#i_want_this").html();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM