Get the App
SLTechnology News&Howtos  ›  Development  › 

The method that jquery binds the same event multiple times

Shulou Source: shulou.com Published: 2022-06-01 07:36:14 09月20日 Update

This article mainly introduces the relevant knowledge of jquery binding the same event many times, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this jquery method article that binds the same event many times. Let's take a look at it.

In the past two days, I encountered using js to dynamically load content and need to bind click events to the added content. At that time, I thought that I could use inline onclick to implement it. Later, instead of doing so, I used on ('click',function () {}). However, when using the on method, I found that when on binds the same event to the same element multiple times, this event will be executed multiple times.

Here is an example of this:

$(function () {)

$(".add") .click (function (eve) {

$(".btn-area") .append ("test button")

/ / $(".test-btn") .off ("click")

$(".test-btn") .on ("click", function () {

Console.log ("test button.")

})

})

})

After clicking the add test button of the add button, the test button binds the click event, and the console station outputs a sentence.

Obviously, the first button was clicked and executed five times.

In this case, you need to unbind the event before each binding event, that is, $(".test-btn") .off ("click");, the off method.

This is the end of this article on "how to bind the same event multiple times by jquery". Thank you for reading! I believe you all have a certain understanding of "how jquery binds the same event many times". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Event method content button knowledge situation time article test obvious one sentence that is value element dynamic at the same time example easy to operate article easy to understand Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Shulou Tech Info MariaDB Redmi