What is the difference between $ (". Anything"). Click () and $ (". Anything"). Bind (click)

What is the difference between $ (". Anything"). click () and $ (". anything"). bind (click)

$(".anything").click(function() {

});

$(".anything").bind('click', function() {

});
+3
source share
4 answers

The first is an abbreviation from the second. The second, actually incorrect, clickshould have been specified. In addition, in the second you have an additional advantage that can be associated with the same function with several events, each of which is separated by a space. For instance.

$(".anything").bind("click keypress blur", function() {

});
+6
source

Nothing click(function() { })is just a shortcut to bind("click", function() { }).

From jQuery docs:

jQuery , .click() .bind('click').

bind() .

+8

.

:

A) .click() no, .

B) .bind() "" :

$(whatever).bind('click.myEvent', function (e) { ... });
$(whatever).unbind('click.myEvent'); // Removes just that handler
+4

, JQuery, , .click(fn) .bind('click', fn): jQuery: $(). click (fn) vs. $(). bind ('click', fn);

, :

  • , ...bind('click focus', fn)
  • , , unbind, ..:
var fn = function() { alert('foo'); }
$('#foo').bind('click', fn);

...

$('#foo').unbind('click', fn);
+2
source

Source: https://habr.com/ru/post/1770227/


All Articles