Home >Web Front-end >JS Tutorial >JQuery two methods to solve the problem that the newly created elements cannot be traversed_jquery

JQuery two methods to solve the problem that the newly created elements cannot be traversed_jquery

WBOY
WBOYOriginal
2016-05-16 15:05:471730browse

Using jQuery’s each method to traverse the newly created elements sometimes does not work.
Needless to say the reason, anyone who often uses JavaScript will know that there are roughly two solutions:

1. As soon as you create the element, use the each method immediately

Copy code The code is as follows:

$('#btn').on("click", function(){
$('#div').append("");
$("input").each(function(){
           //TODO:
});
});

2. Use the setTimeout method and then use the each method

after the page is loaded for a period of time.

Copy code The code is as follows:

setTimeout(function(){
$("input").each(function(){
           //TODO:
});
},1000);
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