jquery 添加<table> 再添加鼠标点击事件

来源:互联网 发布:周琦体测数据 编辑:程序博客网 时间:2024/05/18 18:14

方法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);    });});