Javascript 当类和id动态给定时,单击事件不触发未调用的ans css
大家好,我在动态创建表时声明id和类,如下所示,我有bing使用许多函数使我的点击事件运行,但都在运行中,如果有人可以,请帮助我 这是我的动态表,带有id和类,我必须在“td”单击时调用css,并且应该触发单击事件Javascript 当类和id动态给定时,单击事件不触发未调用的ans css,javascript,jquery,css,dynamic,click,Javascript,Jquery,Css,Dynamic,Click,大家好,我在动态创建表时声明id和类,如下所示,我有bing使用许多函数使我的点击事件运行,但都在运行中,如果有人可以,请帮助我 这是我的动态表,带有id和类,我必须在“td”单击时调用css,并且应该触发单击事件 var tableid=0; // display table....... $('#page').append("<center> <table id='tables' cellspacing='50p
var tableid=0;
// display table.......
$('#page').append("<center> <table id='tables' cellspacing='50px' style='margin-top:70px' >");
for(var j=0;j<row;j++){
$('#tables').append("<tr class='row' id='row"+j+"' style='margin-bottom:50px'>");
for(var k=0;k<5;k++){
if(tableid<tablecount-1){
$("#row"+j).append("<td id='"+tableid+"' class='button' width='150' height='150' background='images/green.png' align='center'>"+
"</td>");
tableid++;
}
}//for ends.
$('#tables').append("</tr'>");
}//for ends
$('#page').append("</center> </table>");
现在我也尝试了.delegate(),它可以工作,但我不能为每个td all传递id(unique)
$('.button').each(function(){
$(this).click(function{
alert("hi");
});
});
$("tr .row > td .button").each(function() {
var $this = $(this);
alert("hi"+$this);
}
/*$("table").delegate($(this),"click",function(){
alert($(this));
printBill(tabid);
});
我不确定我是否理解你的问题
此代码将在每次td单击时执行,变量id将包含td的id。您应该在事件委派中使用
$("table").on("click",function(e){
alert(jquery(e.target).attr('id');
})
你看过firebug中tr的ID了吗 这样分配可能是错误的
id='"+tableid+"'
试试这个
id="+tableid+"
祝你好运。在你的代码中有一个条件
if(tableid是你创建表的方法。你应该这样创建表
$(function(){
var tableid=0;
var row = 5;
// display table.......
var theTable = $('<table />').attr('id', tableid);
for(var j = 0; j < row; j++){
var theRow = $('<tr />').attr('id', 'row'+j).addClass('row');
for(var k=0;k<5;k++){
var theTd = $('<td />').addClass('button').html('test').click(function(){ alert('test') });
theRow.append(theTd);
}
theTable.append(theRow);
}
$('#page').append(theTable);
});
$(函数(){
var-tableid=0;
var行=5;
//显示表。。。。。。。
变量theTable=$('').attr('id',tableid);
对于(var j=0;j
对于(var k=0;kHTML建议:不要使用center
标记,它已被弃用。此外,您按错误的顺序关闭标记。请记住此建议。我认为我们最好使用jquery中带有on功能的事件委派,请让我知道您的建议comment@Ajaybeni当然,我相信是时候让我习惯了。
id="+tableid+"
$('td').click(function(){
var tdId = $(this).attr('id');
console.log(tdId); // You would get the Id of clicked td in teh consloe and now you can use the variable **tdId** for your function.
});
$(function(){
var tableid=0;
var row = 5;
// display table.......
var theTable = $('<table />').attr('id', tableid);
for(var j = 0; j < row; j++){
var theRow = $('<tr />').attr('id', 'row'+j).addClass('row');
for(var k=0;k<5;k++){
var theTd = $('<td />').addClass('button').html('test').click(function(){ alert('test') });
theRow.append(theTd);
}
theTable.append(theRow);
}
$('#page').append(theTable);
});