gpt4 book ai didi

jquery - 为什么 addBack 与 Find() 一起使用

转载 作者:行者123 更新时间:2023-12-01 02:55:59 31 4
gpt4 key购买 nike

我试图理解这段代码的作用,以及为什么我们在不使用 addBack() 的情况下获得相同的结果时将 addBack() 与 find() 一起使用

举这两个例子

$("body div").find("*").addBack().contents().filter(function(){
return this.nodeType === 3 && /shaolin/i.test(this.nodeValue);
})

对比

$("#myDiv").find("*").addBack().contents().filter(function(){
return this.nodeType === 3 && /judo/i.test(this.nodeValue);
})

我们为什么要做/shaolin/i.test(this.nodeValue);?

最佳答案

find() 仅返回与选择器匹配的后代。该代码使用 addBack,以便除了搜索其所有后代元素之外,还将搜索原始 body div 元素的内容。

如果没有一个顶级 DIV 元素与过滤器函数匹配,那么无论有没有 addBack,结果都将相同。代码力求完整,因此不会遗漏任何内容。

该代码正在查找 body div 中包含单词 shaolin 的所有文本节点。 /shaolin 是一个正则表达式,它会根据此表达式测试每个文本节点的值。

关于jquery - 为什么 addBack 与 Find() 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923611/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com