jQuery中bind()和rebind()的作用与常规onclick有什么不同?

时间:2022-08-07 23:56:12

What is the difference between

有什么区别

function bind() {$('#content').click(function(){});}

and

function rebind() {$('#content').click(function(){});}

and simple

而且简单

$('#content').click(function(){});

Or in other words - why do I need to use bind() if I simply want to attach simple click event?

或者换句话说 - 如果我只想附加简单的点击事件,为什么我需要使用bind()?

4 个解决方案

#1


2  

Given a common

鉴于共同点

function func(){ alert('woo!'); }

I think you might've seen something that mentions

我想你可能已经看过一些提到的东西了

$('#content').bind('click', func);

is the same as

是相同的

$('#content').click(func);

Is that what you mean/saw?

这是你的意思/看到了吗?

#2


5  

The advantage of the form .bind() is that you can target multiple events:

形式.bind()的优点是可以定位多个事件:

$("#content").bind("click dblclick focus", function() {
    doSomethingForAllCases();
});

#3


4  

What you've pasted are not jQuery functions, they're just functions in your own code. They both do exactly the same thing, as evidenced by the function bodies being identical.

你粘贴的不是jQuery函数,它们只是你自己代码中的函数。他们都做了完全相同的事情,正如功能体是相同的。

#4


1  

you don't need to use bind for default events like click, onMouse etc..

你不需要为bind,onMouse等默认事件使用bind。

Bind is useful when you are customizing some events and then attaching that event to an element or if you want to attach multiple events at a time.

当您自定义某些事件然后将该事件附加到元素或者您希望一次附加多个事件时,Bind非常有用。

the .bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to .bind() occurs

.bind()方法用于将事件处理程序直接附加到元素。处理程序附加到jQuery对象中当前选定的元素,因此这些元素必须存在于调用.bind()的位置

refer - this

参考 - 这个

#1


2  

Given a common

鉴于共同点

function func(){ alert('woo!'); }

I think you might've seen something that mentions

我想你可能已经看过一些提到的东西了

$('#content').bind('click', func);

is the same as

是相同的

$('#content').click(func);

Is that what you mean/saw?

这是你的意思/看到了吗?

#2


5  

The advantage of the form .bind() is that you can target multiple events:

形式.bind()的优点是可以定位多个事件:

$("#content").bind("click dblclick focus", function() {
    doSomethingForAllCases();
});

#3


4  

What you've pasted are not jQuery functions, they're just functions in your own code. They both do exactly the same thing, as evidenced by the function bodies being identical.

你粘贴的不是jQuery函数,它们只是你自己代码中的函数。他们都做了完全相同的事情,正如功能体是相同的。

#4


1  

you don't need to use bind for default events like click, onMouse etc..

你不需要为bind,onMouse等默认事件使用bind。

Bind is useful when you are customizing some events and then attaching that event to an element or if you want to attach multiple events at a time.

当您自定义某些事件然后将该事件附加到元素或者您希望一次附加多个事件时,Bind非常有用。

the .bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to .bind() occurs

.bind()方法用于将事件处理程序直接附加到元素。处理程序附加到jQuery对象中当前选定的元素,因此这些元素必须存在于调用.bind()的位置

refer - this

参考 - 这个