jQuery on() 和 live

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://apps.bdimg.com/libs/jquery/1.7.0/jquery.js">
</script>
<script>
$(document).ready(function(){
  $("#div1").on("click",function(){
    $(this).css("background-color","pink");
  });
  $("#div2").live("click",function(){
    $(this).css("background-color","pink");
  });
});
</script>
</head>
<body>

<h4 style="color:green;">该实例演示了如何使用 on() 和 live()。</h4>

<div id="div1" style="border:1px solid black;">这是一些文本。
<p>点击此处,使用 <b>on() 方法来设置背景颜色</b>。</p>
</div><br>

<div id="div2" style="border:1px solid black;">这是一些文本。
<p>点击此处,使用 <b>live() 方法来设置背景颜色</b>。</p>
</div>
<p><b>注意:</b>  live() 方法在 jQuery 版本 1.7 中被废弃,在版本 1.9 中被移除。请使用 on() 方法代替</p>
</body>
</html>

  http://www.runoob.com/jquery/event-on.html

http://api.jquery.com/on/

 

posted @ 2017-05-03 12:05  个子  阅读(319)  评论(0编辑  收藏  举报