html:
1
2
3
js:
$("div p").click(function(){
alert(“sss”);
})
与
$("div p").bind("click",function(){
alert("sss");
})
这两种写法有什么区别?
主要的区别有以下两点:
区别一:前者写法当解绑的的时候不易操作,但是后者可直接$("div p").unbind(),
区别二:前者不可实现动态加载得DOM节点的点击事件,但是后者写法可以委托已有元素,实现后面动态添加节点的点击事件。如:$(document.body).on('click',yourselector,function(event){});
没区别,但是bind 可以绑定一系列事件(对同一个元素,绑定单机,双击等,。。。),而click 只能是单击这一个事件
现在的jquery中,统一使用on,
你应该这样问,bind,live,delegate,on的区别
.bind()是直接绑定在元素上
.live()则是通过冒泡的方式来绑定到元素上的。更适合列表类型的,绑定到document DOM节点上。和.bind()的优势是支持动态数据。
.delegate()则是更精确的小范围使用事件代理,性能优于.live()
.on()则是最新的1.9版本整合了之前的三种方式的新事件绑定机制
{event:function, event:function, ...}
你举的例子没有区别,bind()有如下作用:**
jQuery 中的bind()方法:
为被选元素(你的代码中的"div p")添加一个或多个事件处理程序,并规定事件发生时运行的函数(alert("sss"))。
bind 可以同时绑定多个事件:
$('#foo').bind('mouseenter mouseleave', function() {
$(this).toggleClass('entered');
});
bind 可以对同一事件绑定多次:
This is a paragraph.
请点击这里
你举的例子没有区别,bind()有如下作用:
jQuery 中的bind()方法:
为被选元素(你的代码中的"div p")添加一个或多个事件处理程序,并规定事件发生时运行的函数(alert("sss"))。
bind 可以同时绑定多个事件:
$('#foo').bind('mouseenter mouseleave', function() {
$(this).toggleClass('entered');
});
bind 可以对同一事件绑定多次:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").bind("click",function(){
$("p").slideToggle();
});
$("button").bind("click",function(){
alert($("p").text());
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>请点击这里</button>
</body>
</html>
基本没区别只不过bind支持同时多个事件
在你举的这个例子当中没有什么区别
说区别,主要以下:
第二种的bind支持同时多个事件的绑定