如何获取元素的文本节点? [英] How to get the text node of an element?
本文介绍了如何获取元素的文本节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<div class="title">
I am text node
<a class="edit">Edit</a>
</div>
我希望得到我是文本节点,不希望删除编辑标记,并需要跨浏览器解决方案。
I wish to get the "I am text node", do not wish to remove the "edit" tag, and need a cross browser solution.
推荐答案
var text = $(".title").contents().filter(function() {
return this.nodeType == Node.TEXT_NODE;
}).text();
这将获得所选元素的内容
,并对其应用过滤功能。 filter函数仅返回文本节点(即 nodeType == Node.TEXT_NODE
的那些节点)。
This gets the contents
of the selected element, and applies a filter function to it. The filter function returns only text nodes (i.e. those nodes with nodeType == Node.TEXT_NODE
).
这篇关于如何获取元素的文本节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文