Home>

I. Problems:

Today I discovered that the jquery event of an object can be repeatedly bound multiple times,When the event is triggered, the code will be executed multiple times.

Here is an example where the click event is repeatedly bound:

function reg_button_click () {
  $("#button) .click (function () {
    alert ("button click");
  });
}
$(document) .ready (function () {
  #Repeat registration 3 times
  reg_button_click ();
  reg_button_click ();
  reg_button_click ();
  #3 alerts appear when triggered
  $("#button"). click ();
});

Solution:

For scenarios that require repeated binding,Consider using the method of unbind and then bind during event registration;or off and then on

function reg_button_click () {
  $("#button) .unbind (" click "). bind (" click ", (function () {
    alert ("button click");
  });
}
$(document) .ready (function () {
  #Repeat registration 3 times
  reg_button_click ();
  reg_button_click ();
  reg_button_click ();
  #3 alerts appear when triggered
  $("#button"). click ();
});
  • Previous thinkPHP simple traversal array method analysis
  • Next Simple PHP popup confirmation box before deleting