gpt4 book ai didi

javascript - jQuery hasClass 对我不起作用?

转载 作者:行者123 更新时间:2023-11-30 07:02:18 25 4
gpt4 key购买 nike

在下面的代码中,hasClass 不起作用,结果在 alert 中为 false。为什么,我该怎么办?

在线演示: http://jsfiddle.net/Fe3CK/

HTML:

<span>
<div class="adding"></div>
</span>

JS:

alert($('span').hasClass('adding'));

最佳答案

我想你可能误解了什么.hasClass做。它检查元素本身是否分配了这些类之一。来自documentation :

Determine whether any of the matched elements are assigned the given class.

如果要检查元素是否包含具有该类的元素,请使用 .has :

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

alert($('span').has('.adding').length > 0);

关于javascript - jQuery hasClass 对我不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21845845/

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