【发布时间】:2010-09-22 20:40:33
【问题描述】:
我想获取一个元素的所有后代文本节点,作为一个 jQuery 集合。最好的方法是什么?
【问题讨论】:
标签: javascript jquery dom
我想获取一个元素的所有后代文本节点,作为一个 jQuery 集合。最好的方法是什么?
【问题讨论】:
标签: javascript jquery dom
jQuery 没有为此提供方便的功能。您需要将contents() 与find() 结合起来,find() 将仅提供子节点但包含文本节点,而find() 将提供所有后代元素但不提供文本节点。这是我想出的:
var getTextNodesIn = function(el) {
return $(el).find(":not(iframe)").addBack().contents().filter(function() {
return this.nodeType == 3;
});
};
getTextNodesIn(el);
注意:如果您使用的是 jQuery 1.7 或更早版本,上面的代码将不起作用。要解决此问题,请将 addBack() 替换为 andSelf()。从 1.8 开始,andSelf() 已被弃用,取而代之的是 addBack()。
与纯 DOM 方法相比,这有点低效,并且必须包含 ugly workaround for jQuery's overloading of its contents() function(感谢 cmets 中的 @rabidsnail 指出),所以这里是使用简单递归函数的非 jQuery 解决方案。 includeWhitespaceNodes 参数控制空白文本节点是否包含在输出中(在 jQuery 中它们会被自动过滤掉)。
更新:修复 includeWhitespaceNodes 错误时的错误。
function getTextNodesIn(node, includeWhitespaceNodes) {
var textNodes = [], nonWhitespaceMatcher = /\S/;
function getTextNodes(node) {
if (node.nodeType == 3) {
if (includeWhitespaceNodes || nonWhitespaceMatcher.test(node.nodeValue)) {
textNodes.push(node);
}
} else {
for (var i = 0, len = node.childNodes.length; i < len; ++i) {
getTextNodes(node.childNodes[i]);
}
}
}
getTextNodes(node);
return textNodes;
}
getTextNodesIn(el);
【讨论】:
document.getElementById():var div = document.getElementById("foo"); var textNodes = getTextNodesIn(div);
.contents() 的使用意味着它也会搜索 iframe。我不明白它怎么可能是一个错误。
Jauco 在评论中发布了一个很好的解决方案,所以我在这里复制它:
$(elem)
.contents()
.filter(function() {
return this.nodeType === 3; //Node.TEXT_NODE
});
【讨论】:
$('body').find('*').contents().filter(function () { return this.nodeType === 3; });
【讨论】:
jQuery.contents() 可以与jQuery.filter 一起使用以查找所有子文本节点。稍加改动,您也可以找到孙子文本节点。不需要递归:
$(function() {
var $textNodes = $("#test, #test *").contents().filter(function() {
return this.nodeType === Node.TEXT_NODE;
});
/*
* for testing
*/
$textNodes.each(function() {
console.log(this);
});
});
div { margin-left: 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="test">
child text 1<br>
child text 2
<div>
grandchild text 1
<div>grand-grandchild text 1</div>
grandchild text 2
</div>
child text 3<br>
child text 4
</div>
【讨论】:
我得到了很多带有接受过滤功能的空文本节点。如果您只对选择包含非空格的文本节点感兴趣,请尝试将 nodeValue 条件添加到您的 filter 函数中,例如简单的 $.trim(this.nodevalue) !== '':
$('element')
.contents()
.filter(function(){
return this.nodeType === 3 && $.trim(this.nodeValue) !== '';
});
或者为了避免内容看起来像空格但不是的奇怪情况(例如软连字符&shy; 字符、换行符\n、制表符等),您可以尝试使用正则表达式。例如,\S 将匹配任何非空白字符:
$('element')
.contents()
.filter(function(){
return this.nodeType === 3 && /\S/.test(this.nodeValue);
});
【讨论】:
如果您可以假设所有子节点都是元素节点或文本节点,那么这是一种解决方案。
将所有子文本节点作为一个 jquery 集合获取:
$('selector').clone().children().remove().end().contents();
要获得原始元素的副本,其中非文本子元素已删除:
$('selector').clone().children().remove().end();
【讨论】:
由于某种原因,contents() 对我不起作用,所以如果它对您不起作用,这是我制定的解决方案,我创建了 jQuery.fn.descendants,并选择是否包含文本节点
用法
获取所有后代,包括文本节点和元素节点
jQuery('body').descendants('all');
获取所有仅返回文本节点的后代
jQuery('body').descendants(true);
获取所有仅返回元素节点的后代
jQuery('body').descendants();
Coffeescript 原创:
jQuery.fn.descendants = ( textNodes ) ->
# if textNodes is 'all' then textNodes and elementNodes are allowed
# if textNodes if true then only textNodes will be returned
# if textNodes is not provided as an argument then only element nodes
# will be returned
allowedTypes = if textNodes is 'all' then [1,3] else if textNodes then [3] else [1]
# nodes we find
nodes = []
dig = (node) ->
# loop through children
for child in node.childNodes
# push child to collection if has allowed type
nodes.push(child) if child.nodeType in allowedTypes
# dig through child if has children
dig child if child.childNodes.length
# loop and dig through nodes in the current
# jQuery object
dig node for node in this
# wrap with jQuery
return jQuery(nodes)
加入 Javascript 版本
var __indexOf=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++){if(t in this&&this[t]===e)return t}return-1}; /* indexOf polyfill ends here*/ jQuery.fn.descendants=function(e){var t,n,r,i,s,o;t=e==="all"?[1,3]:e?[3]:[1];i=[];n=function(e){var r,s,o,u,a,f;u=e.childNodes;f=[];for(s=0,o=u.length;s<o;s++){r=u[s];if(a=r.nodeType,__indexOf.call(t,a)>=0){i.push(r)}if(r.childNodes.length){f.push(n(r))}else{f.push(void 0)}}return f};for(s=0,o=this.length;s<o;s++){r=this[s];n(r)}return jQuery(i)}
未缩小的 Javascript 版本:http://pastebin.com/cX3jMfuD
这是跨浏览器,代码中包含一个小的Array.indexOf polyfill。
【讨论】:
也可以这样:
var textContents = $(document.getElementById("ElementId").childNodes).filter(function(){
return this.nodeType == 3;
});
以上代码从给定元素的直接子子节点中过滤 textNodes。
【讨论】:
如果你想去掉所有标签,那么试试这个
功能:
String.prototype.stripTags=function(){
var rtag=/<.*?[^>]>/g;
return this.replace(rtag,'');
}
用法:
var newText=$('selector').html().stripTags();
【讨论】:
对我来说,普通的旧 .contents() 似乎可以返回文本节点,只需要小心你的选择器,这样你就知道它们将是文本节点。
例如,这用pre标签包裹了我表中所有TD的文本内容,没有问题。
jQuery("#resultTable td").content().wrap("<pre/>")
【讨论】:
我遇到了同样的问题并解决了:
代码:
$.fn.nextNode = function(){
var contents = $(this).parent().contents();
return contents.get(contents.index(this)+1);
}
用法:
$('#my_id').nextNode();
类似于next(),但也返回文本节点。
【讨论】: