How to solve jquery bind failure problem

藏色散人
Release: 2021-11-12 11:23:13
Original
2154 people have browsed it

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.

How to solve jquery bind failure problem

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数据代码
});
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template