需要指定ID及其更改的类名以将其更改回来。我正在HTML自动点唱机上的3个按钮之间切换。所以如果在选择它之后再次点击相同的按钮(摇滚) - 摇滚会沉默。附加到ID的JQuery ID更改类然后通过ID和新类名重新获取相同的项目
按钮动作来启动按钮(1 3的按钮)
$("a#butblue").click(function(){
stopall();
$.fn.soundPlay({url: '../audio/lounge.mp3', playerId: 'embed_playerB', command: 'play'});
$("a#butblue").removeClass("blue_off");
$("a#butgreen").removeClass("green_on");
$("a#butgreen").addClass("green_off");
$("a#butorange").removeClass("orange_on");
$("a#butorange").addClass("orange_off");
$("a#butblue").addClass("blue_on");
});
function stopall() {
$.fn.soundPlay({playerId: 'embed_playerB', command: 'stop'});
$.fn.soundPlay({playerId: 'embed_playerG', command: 'stop'});
$.fn.soundPlay({playerId: 'embed_playerO', command: 'stop'});
}
现在,如果一个#butblue.blue_on再次单击我希望它关闭。试图使用以下,但不知何故,我似乎没有连接ID和更改类的名称。我可以看到他们在萤火虫,它正在改变班级。只是似乎无法得到它的处理。这可以做到吗? (它必须是)。
:
$("a#butblue.blue_on").click(function(){
stopall();
$.fn.soundPlay({playerId: 'embed_playerB', command: 'stop'});
$("a#butblue").removeClass("blue_on");
$("a#butblue").addClass("blue_off");
});
完美。感谢您指出了这一点。 hasClass正是需要的。每小时学习一些新东西。 – Stuff
有一件事要指出,toggleClass可能是一个更好的解决方案,正如@Adam Terison在其他答案中指出的那样。通过这种方式,您可以避免杂耍课程,如果您错误地忘记删除课程,则最终可能会同时添加blue_on和blue_off。但基本思想仍然适用于hasClass() – omarello
随着这个在我的口袋里肯定会使用切换下次我得到这样的情况。谢谢。 – Stuff