我有以下小提琴: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;此单击的块成为选定的块。然后我想单击以选择另一个块。如果这两个块的样式 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
它似乎不起作用。
谢谢!