gpt4 book ai didi

jQuery hasClass() - 检查多个类

转载 作者:IT王子 更新时间:2023-10-29 03:24:14 25 4
gpt4 key购买 nike

与:

if(element.hasClass("class"))

我可以检查一个类,但是有没有一种简单的方法来检查“元素”是否有许多类中的任何一个?

我正在使用:

if(element.hasClass("class") || element.hasClass("class") ... )

还不错,但我在想类似的事情:

if(element.hasClass("class", "class2")

不幸的是,这不起作用。

有这样的东西吗?

最佳答案

element.is('.class1, .class2')

有效,但比

慢 35%
element.hasClass('class1') || element.hasClass('class2')

JSperf chart showing .hasClass() is faster than .is()

另见 this jsbench.me测试。

关于jQuery hasClass() - 检查多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2214952/

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