jQuery查找带有文本的所有元素 [英] Jquery find all elements with text

查看:100
本文介绍了jQuery查找带有文本的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扫描所有DOM,找到任何具有文本并将其包装在span类中的最佳方法是什么? 谢谢

What would be the best way to scan trough all the DOM, find any element that have text and wrap it in a span class? Thanx

推荐答案

要包装所有包含除空格以外的所有文本节点:

To wrap all text nodes that contain something other than just whitespace:

$('body *').contents().filter(function() { 
    return (this.nodeType == 3) && this.nodeValue.match(/\S/); 
}).wrap("<span />")

要包装所有文本节点,包括仅包含空格的文本节点:

To wrap all text nodes, including those that contain only whitespace:

$('body *').contents().filter(function() { 
    return (this.nodeType == 3) && this.nodeValue.length > 0; 
}).wrap("<span />")

这篇关于jQuery查找带有文本的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆