Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method that jquery binds the same event multiple times

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report