我的html是:
<div>
Some text
<span>label text</span>
</div>
Jquery:
??
我希望输出打印为 -
"Some text"
当我这样做时$("div").text()
,我得到Some textLabel text
当我这样做时$("div").html()
,我得到Some text<span>Label text</span>
最佳答案
请尝试这个:
工作演示 http://jsfiddle.net/f8VFf/您还可以查找 innerHTMl
和 outterHTML
jQuery.fn.justtext = function() {
return $(this).clone()
.children()
.remove()
.end()
.text();
};
alert($('div').justtext());
关于jquery - 如何仅获取元素中的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11537708/