jquery 动态添加onclick事件

来源:互联网 发布:显卡优化不了游戏 编辑:程序博客网 时间:2024/05/19 09:13
<!DOCTYPE html>
<html>
<head>
<title>练习选择器</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<p>
 <button class="b">test</button>
 <button class="b1">test1</button>
</p>
<script type="text/javascript">
  $(".b1").click(function(){
    $(".b").bind("click",function() { test(1) });
  })
  function test(i){alert(i)}
</script>
</body>

</html>


这样就可以通过点击test1给test添加点击事件了。这是有参数的情况如果不需要传递参数那么可以改成这样

  $(".b").bind("click",test );

如果写成

 $(".b").bind("click",test() );或者

 $(".b").bind("click",test(1));这就变成了函数调用了。

0 0