Jquery用文本查找所有元素

Mir*_*cea 10 jquery text

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

Mar*_*ger 14

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

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

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

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