所以我想動態生成一些框在屏幕上使用divs,當你點擊一個特定的(name=box1
)它執行某些代碼。當他們被硬編碼到我的html下面的代碼工作正常,但現在,因爲我把它們包裝在一個p
它需要'this'作爲參考p
而不是div
。我相信它的第11行需要改變。引用一個特定的元素時,它被包裹在另一個
$(document).ready(function(){
$('#swapboxes').click(function(){
//build the box location array and boxes
$('#boxeshere').html("");
for(var i = 0;i < $.gameconfig.numofboxes;i++){
$('<div class="boxes" style="background:#bf3215;height:100px;width:100px;left:'+200*i+'px;position:fixed;" name="box' + i + '" id="' + i + '"/>').appendTo('#boxeshere');
}
});
//Execution for clicking on boxes
$('.boxes').click(function(){
if(this.attributes["name"].value == "box1"){
$("#info").text("Congrats!!! You win!");
}
else{
$("#info").text("I'm sorry, wrong box");
}
});
});
您的問題中沒有足夠的信息供任何人回答。 –