Solution to jquery bind failure: 1. Create an HTML sample file; 2. Use "$(".sentnum-box").delegate(".a-add-ordergoods","click", function(){...});" method can be solved.
The operating environment of this article: windows7 system, jquery version 3.2.1, DELL G3 computer
How to solve the problem of jquery bind failure?
jQuery uses bind dynamic binding event invalid processing method
I am currently developing a page. When doing page special effects, I need to assign an event to a dynamically loaded button
So I used $(obj).bind(); to bind the event without thinking.
But there is a problem:
bind can indeed bind events, but that is compared to fixed html tags
When the page content is dynamically loaded , there is a bug in the bind event. It can only be bind once, and it will be useless when you trigger the event for the second time.
For example:
I assign a click to the tag , tags including content are read from background data and then loaded dynamically. When I use bind to bind the click event, the first click can successfully trigger
But the second click, the click event is invalid.
Later research found that jQuery still There is a method to bind events: delegate();
The usage is as follows:
$(".sentnum-box").delegate(".a-add-ordergoods","click",function(){ //js数据代码 });
This way you can bind events to dynamic data and never expire
Recommended Study: "jquery video tutorial"
The above is the detailed content of How to solve jquery bind failure problem. For more information, please follow other related articles on the PHP Chinese website!