<body> <ul> <li>111</li> <li>222</li> <li>333</li> </ul> <button id="btn" value="click"></button> <script> $("#btn").on("click",function(){ $('<li>'+Math.random()+'</li>').appendTo("ul"); }); $("li").on("click",function(){ console.log($(this).html()); }); </script> </body>
<body> <ul> <li>111</li> <li>222</li> <li>333</li> </ul> <button id="btn" value="click"></button> <script> $("#btn").on("click",function(){ $('<li>'+Math.random()+'</li>').appendTo("ul"); }); // $("li").on("click",function(){ // console.log($(this).html()); // }); $("ul").on("click",function(e){ if(e.target.tagName =="LI"){ // console.log(e.target.innerHTML); console.log($(e.target).html()); } }) </script> </body>
$("ul").on("click","li",function(){ //li才是实际要触发的元素 console.log($(this).html());//this代表当前点击的li });
5.hover
文章来源: 04-4 jquery事件