gpt4 book ai didi

jquery - 当找到元素时,has() 到底返回什么

转载 作者:行者123 更新时间:2023-12-01 04:05:48 25 4
gpt4 key购买 nike

嘿伙计们,我正在尝试构建一个小片段来告诉我给定的父元素是否有子元素,我仍然只是在调试东西,以了解 Jquery 和 JS 一般如何工作。这是我到目前为止所拥有的:

$(document).ready(function(){

var str_dv = $('#la');
$('p , span').on('click' , function(e){


// console.log( str_dv,e.target , str_dv.has(e.target), !str_dv.has(e.target).length)
if (str_dv.has(e.target).length > 0) {
console.log('has the element');
}

});
});

我只是好奇,但是当我说parent.has(child)时,如果找到了 child ,has()会返回什么,如果没有找到 child ,它会返回什么?

我在 SO 中搜索了类似的问题,但找不到,甚至连 Jquery 文档都找不到。

如果我查看 Jquery 源代码,我会发现:

"has": markFunction(function( selector ) {
return function( elem ) {
return Sizzle( selector, elem ).length > 0;
};
}),

这对我来说没有多大意义。

所以再次回到我的问题,has() 返回什么? (当找到传递的元素时以及未找到时)?

最佳答案

has() 返回什么?

根据文档,

Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.

因此,has 通过其子元素(其选择器传入 has )的元素来过滤匹配集中的元素。

例如选择器是:

$("li").has("ul");

这将仅选择其中包含 ul 元素的 li 元素。

<强> Demo For has selector

关于jquery - 当找到元素时,has() 到底返回什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758832/

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