<table id="table">
<tr>
<td>11111</td>
</tr>
</table>
<script>
setTimeout(function() {
$('#table').append('<tr><td class="test" id="12">id12</tr><tr><td class="test" id="13">id13</tr><tr><td class="test" id="14">id14</tr>')
}, 1000)
$('#table').on('click', 'td.test', function() {
var test_id = $(this).attr('id')
console.log(test_id)
})
</script>
ajax生成的未来元素。给未来元素添加事件,可以使用jq的on方法。或者,document.body.onclick=function(e){
var e=e||window.evetn
if(e.target.tagName=='xxxx') //或者.className=='xxxxx'
}
意思是当点击的元素的标签名或class名为什么什么的时候就怎么怎么的。
事件是在网页加载的时候都绑定好了,动态添加的tr上其实并没有绑定事件,如果想动态绑定的话可以用live来绑定,live方法用法和bind相同。比如想给所有tr动态绑定click事件,$("tr").live("click",function(){/*…………*/}) ,强烈建议你在动态添加tr后给新添加的tr单独绑定一次事件欢迎分享,转载请注明来源:内存溢出
评论列表(0条)