向元素添加点击事件处理程序,是.bind('click')
还是.click
更好?有什么区别?任何性能方面?
问问题
150 次
1 回答
5
There is no difference. Internally, click
just calls on
, and bind
also just calls on
. So for a very minor speed increase, just use on
:
$("#someId").on("click", function () {
//Do stuff
});
Here's the relevant part of the jQuery 1.7.2 source for the .click()
method:
return arguments.length > 0 ? this.on(name, null, data, fn) : this.trigger(name);
And the source of the .bind()
method:
return this.on(types, null, data, fn);
If you're using jQuery below version 1.7...
Note that the .on()
method was introduced in jQuery 1.7. If you are using an older version, .click()
will internally just call .bind()
instead. Here's the source of .click()
from 1.6.2:
return arguments.length > 0 ? this.bind(name, data, fn) : this.trigger(name);
于 2012-07-11T09:04:34.990 回答