JQuery click 与 on('click', ...)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
  
<script type="text/javascript">
    $(function() {
      $('.content').append('<li>FlyElephant</li>');
 
/*原生的正常,追加的没反应
      $('.content li').click(function(event) {
          console.log('博客园-FlyElephant');
      });
*/	  

/*原生的正常,追加的没反应
      $('.content li').on('click', function() {
          console.log('博客园-FlyElephant');
      });
*/	  
	  
      /*原生的、追加的都正常*/
      $(document).on('click', '.content li', function() {
          console.log('keso');
      });
 
      $('.test').click(function() {
          $('.content').append('<li>keso</li>');
      });
    }); 
</script>

</head>
 
<body>
<ul class="content">
</ul>
<span class="test">点击我追加内容</span
</body>
 
</html>

猜你喜欢

转载自blog.csdn.net/jinjiankang/article/details/80862327