需要指定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