gpt4 book ai didi

javascript - jquery 如果有类问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:52 25 4
gpt4 key购买 nike

我正在使用 bootstrap 选项卡,如果单击 LI 我想用 javascript 执行一些功能,但是有条件的,如果它处于事件状态,什么都不做,否则执行此操作...

我想出了这段代码来检查它是否有一个class="active"因为bootstrap tabs adds class="active"to LI for the active tab但是它不能很好地工作,它总是返回 true,我在这里做错了什么?

代码

var i = $( "li" ).hasClass( "active" );

$( "li" ).click(function() {
if (i == true ) {
console.log("the tab is already active");
}
else {
console.log("selected");
}
});

这是 jsfiddle demo

最佳答案

使用$(this)获取当前对象

$( "li" ).click(function() {

if ($(this).hasClass("active")) {
console.log("the tab is already active");
}
else {
console.log("selected");
}

});

Fiddle

关于javascript - jquery 如果有类问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27599189/

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