為動態添加的元素綁定事件


<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
  $("body").on('click','p',function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<p>如果您點擊我,我會消失。</p>
<p>點擊我,我會消失。</p>
<p>也要點擊我哦。</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<p>如果您點擊我,我會消失。</p>
<p>點擊我,我會消失。</p>
<p>也要點擊我哦。</p>
</body>
</html>

注意1,2代碼區別,方式1,后期動態添加的<p>標簽是可以點擊消失的,方式2不行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM