私は次のフィドルを持っています:http: //jsfiddle.net/mauricederegt/HMkcD/1/
一連の色付きのブロック(div)が含まれています。1をクリックしてブロックを表示します。生成されるHTMLは次のようになります。
<div id="plane">
<div class="tile tile1" block-id="1" style-id="1" style="left:0px; top:0px"></div>
<div class="tile tile2" block-id="2" style-id="2" style="left:100px; top:0px"></div>
<div class="tile tile1" block-id="3" style-id="1" style="left:200px; top:0px"></div>
<div class="tile tile2" block-id="4" style-id="2" style="left:0px; top:100px"></div>
<div class="tile tile1" block-id="5" style-id="1" style="left:100px; top:100px"></div>
<div class="tile tile2" block-id="6" style-id="2" style="left:200px; top:100px"></div>
<div class="tile tile1" block-id="7" style-id="1" style="left:0px; top:200px"></div>
<div class="tile tile2" block-id="8" style-id="2" style="left:100px; top:200px"></div>
<div class="tile tile1" block-id="9" style-id="1" style="left:200px; top:200px"></div>
<div class="tile tile3" block-id="10" style-id="3" style="left:50px; top:50px"></div><div class="tile tile4" block-id="11" style-id="4" style="left:150px; top:50px"></div>
<div class="tile tile4" block-id="12" style-id="4" style="left:50px; top:150px"></div>
<div class="tile tile3" block-id="13" style-id="3" style="left:150px; top:150px"></div>
</div>
ss
ブロックをクリックしたときに、色付きのブロック/divにクラスを追加したい。このクリックされたブロックが選択されたブロックになります。次に、クリックして別のブロックを選択します。これら2つのブロックのスタイルIDが同じである場合、それらのブロックを削除したいと思います。ss
そうでない場合は、クラスを再度削除します。
これを行うために、次のコードを作成しました。
$('#plane').click(function(){ //the click function
var clickedBlock = $(this); //get the data of the clicked div/block
clickedBlock.addClass('ss'); //add class ss to the clicked div/block
if (blockSelected) { //if div/block is selected
if ($(blockSelected).attr('block-id') == clickedBlock.attr('block-id')) { //if block-id of the selected div equals the block-id of the clicked div
clickedBlock.removeClass('ss'); //remove class ss
} else { //else
if ($(blockSelected).attr('style-id') == clickedBlock.attr('style-id')) { //if style selected div equals style clicked div
$(blockSelected).addClass('x'); //ad class x to selected div or better: remove div
clickedBlock.addClass('x'); //ad class x to clicked div or better: remove div
totalTiles = totalTiles - 2; //deduct 2 of the total tiles
} else { //if not equal styles
$(blockSelected).removeClass('ss'); //remove class ss form the selected div
clickedBlock.removeClass('ss'); //remove class ss from the clicked div
}
}
blockSelected = null;
} else {
blockSelected = this;
}
});
問題は、それを機能させることができないということです。最初は正しくないと思いますが、うまくいかないように見える#plane
ので、そこにどのコードを入れるべきかわかりません。#plane div
ありがとうございました!