How to implement binding events for dynamically generated HTML tags

零到壹度
Release: 2018-03-24 11:28:52
Original
2470 people have browsed it

How to bind events to the generated HTML tags after dynamically using js to generate tags in HTML? After trying many methods, it just can't be bound. Let's follow the editor to see the solution today.

Unsuccessful case:

$("#id").change(function(){
    console.log('需要执行的事情');
});
Copy after login

It was found that using the above method, the tag with ID='id' in the HTML tag generated by JS could not be bound. change event; after thinking for a long time, I finally found a method;

Correct approach:

$(document).change("#id",function(){console.log("需要执行的事情")
});
Copy after login

I found that this is the only way magnetic energy can bind events to it, and other events can also be bound in this way

Related recommendations:

Event binding issues for dynamically generated content in HTML

In HTML Solution about the inability to bind js events to dynamically created tags

About the inability to bind events to dynamically generated tags

The above is the detailed content of How to implement binding events for dynamically generated HTML tags. 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