只想获得一个嵌套范围标记的内部html ..
<span class="select2-selection__rendered" id="select2-SearchByUser-container" title="chowdhury , nayan (nayanchowdhury92@gmail.com)">
<span class="select2-selection__clear">×</span>
mark john
</span>
我需要mark john
,我尝试=&gt;
alert($('#select2-SearchByUser-container').html())
我提供类似=&gt;
的输出/警告<span class="select2-selection__clear">×</span>mark john
有什么方法我只能得到mark john
...请帮助...
答案 0 :(得分:0)
要执行您需要的操作,您可以检索元素中的最后一个textNode,如下所示:
var text = $('#select2-SearchByUser-container').contents().filter(function() {
return this.nodeType == 3 && this.nodeValue.trim();
}).last().text().trim();
console.log(text);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="select2-selection__rendered" id="select2-SearchByUser-container" title="chowdhury , nayan (nayanchowdhury92@gmail.com)">
<span class="select2-selection__clear">×</span> mark john
</span>
&#13;
答案 1 :(得分:0)
试试这个:
https://jsfiddle.net/4nwp25Le/
jQuery(document).ready(function() {
var node = $('#select2-SearchByUser-container').contents().filter(function() {
return this.nodeType == 3; // text node
});
alert(node.text());
});
答案 2 :(得分:0)
到目前为止,你的html并不好,如果可能的话,你可以为它们设置不同的范围。
如果您无法控制HTML,可以尝试这样的
$('#select2-SearchByUser-container').clone().children().remove().end().text()
答案 3 :(得分:0)
var el = document.getElementById("select2-SearchByUser-container"),
child = el.firstChild,
texts = [];
while (child) {
if (child.nodeType == 3) {
texts.push(child.data);
}
child = child.nextSibling;
}
var text = texts.join("");
alert(text);
&#13;
<span class="select2-selection__rendered" id="select2-SearchByUser-container" title="chowdhury , nayan (nayanchowdhury92@gmail.com)">
<span class="select2-selection__clear">×</span>
mark john
</span>
&#13;