我在 jquery 对象中有一段 html。当我说 $(this).html() 时,我得到:
<span class="value">4</span><span class="type">teaspoons</span>butter
我只想从这个 html 片段中获取不在跨度中的文本片段。在这个例子中,它是黄油。
我如何得到它?
谢谢!
最佳答案
有一个简单但作弊的方法,删除所有子元素并获取文本属性
var tmp = $(".post-text").clone();
tmp.children().remove();
tmp.text();
编辑还有一个Text Children plugin就是这样做的。
关于jquery - 获取封装在 jquery 对象中的 html 元素的直接内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2626750/