gpt4 book ai didi

javascript - jQuery 检查类是否存在或有多个类

转载 作者:行者123 更新时间:2023-12-02 16:51:47 25 4
gpt4 key购买 nike

我有一个由 cms 生成的类 js-bootstrap3。我需要做的是检查包含元素是否只有 js-bootstrap3.unwrap() 内容,但该元素是否具有多个包含 的类js-bootstrap3 然后我尝试删除该类。

jsFiddle

$('.jsn-bootstrap3').each(function(){
if( $(this).attr('class') !== undefined && $(this).attr('class').match(/jsn-bootstrap3/) ) {
console.log("match");
$(this).contents().unwrap();
$(this).removeClass('jsn-bootstrap3');
}
});

这似乎只是检测任何以 js-bootstrap3 作为类的元素并将其解开。

最佳答案

this.className 是一个包含元素所有类的字符串(以空格分隔),因此如果它不仅仅是 "jsn-bootstrap3" 你知道它有多个类:

$('.jsn-bootstrap3').each(function(){
if( $.trim(this.className) !== "jsn-bootstrap3") {
// Just jsn-bootstrap3
$(this).contents().unwrap();
} else {
// More than just jsn-bootstarp3
$(this).removeClass('jsn-bootstrap3');
}
});

关于javascript - jQuery 检查类是否存在或有多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26546071/

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