日期:2014-05-16  浏览次数:20377 次

请求:jquery 添加<table> 再添加鼠标点击事件???
小弟新手,请求用jquery动态添加一个<table> 然后再进行鼠标操作,怎么做?我试了怎么不行?改怎么样获取?谢谢!!!
html: <div id="bodyDiv">
   <input value="ADD_DIV" type="button" id="addDivBtn"/>
   <div id="addDiv"></div>
       </div>

js:   $(document).ready(function(){
$("#addDivBtn").click(function(){
           var shtml = "<table><tr><th><input value='HELLO WORLD' type='button' id='thBtn')/></th><th>HELLO</th><th>WORLD</th></tr></table>";
$("#addDiv").html(shtml);
});

      $("#thBtn").click(function(){
alert("test");
});
       });

------解决方案--------------------
$("#thBtn").live('click',function(){
alert("test");
});

------解决方案--------------------
方法1
$(document).ready(function(){
$("#addDivBtn").click(function(){
var shtml = "<table><tr><th><input value='HELLO WORLD' type='button' id='thBtn')/></th><th>HELLO</th><th>WORLD</th></tr></table>";
$("#addDiv").html(shtml);
});

      $("#addDiv").on('click','#thBtn',function(){
alert("test");
});
       });

------解决方案--------------------
2
$(document).ready(function(){
$("#addDivBtn").click(function(){
var shtml = $("<table><tr><th><input value='HELLO WORLD' type='button' id='thBtn')/></th><th>HELLO</th><th>WORLD</th></tr></table>");
shtml.find("#thBtn").click(function(){
alert('cc');
})
$("#addDiv").append(shtml);
});

       });