Home  >  Article  >  Web Front-end  >  How to implement binding events for dynamically generated HTML tags

How to implement binding events for dynamically generated HTML tags

零到壹度
零到壹度Original
2018-03-24 11:28:522446browse

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('需要执行的事情');
});

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("需要执行的事情")
});

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!

Statement:
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