Javascript 按钮打开和关闭单击事件
我正在尝试使用Javascript 按钮打开和关闭单击事件,javascript,jquery,Javascript,Jquery,我正在尝试使用addClass和removeClass,以便使用比较图表进行进一步处理 代码: 问题是,当用户再次单击按钮(将其设置为关闭状态)时,图表不会关闭 你能提供一个参考吗?不确定chart()的要点是什么,但是 试试这个: $('li').click(function () { if ($(this).attr("class") == "Btn1") { $(this).addClass('selected'); } else { $('
addClass
和removeClass
,以便使用比较图表进行进一步处理
代码:
问题是,当用户再次单击按钮(将其设置为关闭状态)时,图表不会关闭
你能提供一个参考吗?不确定
chart()
的要点是什么,但是
试试这个:
$('li').click(function () {
if ($(this).attr("class") == "Btn1") {
$(this).addClass('selected');
} else {
$('.selected').removeClass('selected');
}
});
试试这个:
if ($(this).attr("class") == "Btn1 selected"){
$(this).removeClass("selected");
alert($(this).attr("class")); //alert output -->Btn1
}else if($(this).attr("class") == "Btn1"){
$(this).addClass("selected");
alert($(this).attr("class")); //alert output -->Btn1 selected
}
你需要为click on off按钮编写代码的else部分或toggle类在哪里?我真的不明白。。。你能在你的问题上多加努力,把问题解释清楚一点吗?你能告诉我们你的情况吗?我的意思是
chart()
做什么?我们应该猜猜吗?我的意思是脱离图表不会发生。不确定这意味着什么基本上,单击按钮时我正在显示图表,当同一个按钮单击时,图表不应显示。(test==true?$(.Btn1”).show():$(.Btn1”).hide();谢谢你,宝贝和西尔兹。。。。这两个准则都明确提出了符合这一要求的想法。再次感谢!
if ($(this).attr("class") == "Btn1 selected"){
$(this).removeClass("selected");
alert($(this).attr("class")); //alert output -->Btn1
}else if($(this).attr("class") == "Btn1"){
$(this).addClass("selected");
alert($(this).attr("class")); //alert output -->Btn1 selected
}