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

How to solve the problem of jquery bind failure

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to solve the jquery bind failure problem", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to solve the jquery bind failure problem" this article.

The solution to jquery bind failure: 1, create a HTML sample file; 2, use "$(" .sentnum-box "). Delegate (" .a-add-ordergoods "," click ", function () {.});" method can be solved.

This article operating environment: windows7 system, jquery3.2.1 version, DELL G3 computer

How to solve the problem of jquery bind failure?

JQuery uses invalid handling methods for bind dynamic binding events

Recently, in page development, when doing page special effects, you need to assign an event to a dynamically loaded button.

So bind the event with $(obj) .bind (); without thinking.

But there is a problem:

Bind does bind events, but that's relative to fixed html tags.

When the page content is dynamically loaded, there is a bug for the bind event, which can only be bind once, but it is useless when you trigger the event the second time.

For example:

I assign a click to the tag, and the tag, including the content, is read from the background data and loaded dynamically. When I use bind to bind click events, the first click on click successfully triggers

But the second click, the click event is invalid.

Later research found that jQuery also has a way to bind events: delegate ()

The usage is as follows:

$(".sentnum-box") .delegate (".a-add-ordergoods", "click", function () {/ / js data code})

In this way, dynamic data binding events can be implemented and will never fail.

The above is all the contents of the article "how to solve the problem of jquery bind failure". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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