[英]Where to put onclick … or should I be using .click()
I am trying to make a rudimentary GUI for a memory game. 我正在尝试制作一个用于记忆游戏的基本GUI。 I am at a loss as to how to get the id of each td to display with onclick.
我不知道如何获取每个td的ID以与onclick一起显示。 I don't think I am thinking rightly about when things are happening with var cellID.
我不认为我在正确地考虑var cellID何时发生问题。 The rest of this works.
其余的工作。 See comments.
看评论。
function cid(){alert(cellID);}//**<-- I feel like this shouldn't be here.**
function makeBoard(h,w){
var $tab, $row, $cell;
$tab = $('<div>')
.attr('id','game')
.appendTo('#memorygame');
for(var i = 0; i < h; i++){
$row = $('<tr>')
.appendTo($tab);
for(var j = 0; j <w; j++){
var cellID = ('row'+i+'col'+j)
$cell = $('<td onclick="cid()">')//**<-- this isn't working**
.attr('id',cellID)
.appendTo($row);
}
}
}
$(makeBoard);
$cell = $('<td>')
.attr('id',cellID)
.click(function(){
alert($(this).attr('id'));
})
.appendTo($row);
Or insetad of alert you can use 或您可以使用的insetad警报
$('#whereyouwanttodisplay').html($(this).attr('id'));
I don't think your using a valid selector in the jquery function. 我不认为您在jquery函数中使用有效的选择器。 Try creating the td first then add the attribut like this.
尝试先创建td,然后像这样添加属性。 $('').attr('onclick' , 'cid()')
$('')。attr('onclick','cid()')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.