[英]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]")? 如何在元素之间撤回文本(不是元素的内部文本,在这种情况下我应该得到“[我想得到这个文本]”)?
I think the simplest way would be to deal with the native dom elements: 我认为最简单的方法是处理本机dom元素:
var a = $(".container a")[0];
var textNode = a.nextSibling;
var text = textNode.textContent;
Note that var text = textNode.nodeValue;
注意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: 一种方法是将部件包含在span标签中并获取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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.