別のクラスをクリックしたときにクラスをdiv
に追加しようとしています。 1つをクリックするとコードが表示されますdiv
すべてがクラスを追加しました。クリックした要素とその子にのみクラスを追加する
私のjQueryのは、次のとおりです。
$(".front").click(function (event) {
// remove classes from all
$('.front .back').not($(this)).removeClass('back-Active');
// add class to the one we clicked
$('.front .back').addClass("back-Active");
event.stopPropagation();
});
どのように私はこの問題を解決するだろうか?あなたも.front
要素にクラスを追加したい場合は、現在の要素
$(".front").click(function (event) {
// remove classes from all
$('.front .back').removeClass('back-Active'); //line changed to remove back-Active from all
// add class to the one we clicked
$(this).find('.back').addClass("back-Active"); //this line changed to add back-Active to only current element
event.stopPropagation();
});
へthis
参照を使用することにより
OPだけではなく、子供、あまりにもクリックされた( '.front')要素にクラスを追加する - ' .back'。 – Tushar
@ Tusharはコードを更新し、フロント要素も同じクラスを持つことに気付かなかった。ありがとう – gurvinder372