gpt4 book ai didi

javascript - 将一个 div 附加到动态创建的 div

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:02 25 4
gpt4 key购买 nike

我正在迭代一个数组并动态创建一个 div,如下所示:

   $.each(item,function(k,item2) {
html = '<td><div id="test">'+item2.Name +'</div></td>'

//For just testing purpose , I am adding a div dynamically as:
$("#test").append("<div class='test class'></div>");
});

但最后当我浏览 html 页面时,根本找不到带有 class='test class' 的新 div。有什么我想念的吗?

编辑:在 html 标签“html”中,有多个 div,我想通过 id 附加到特定的 div

不太确定是什么问题:但是在创建 html 之后, $("#test").append("<div class='test class'></div>");有效,但附加使用 $(html).find() 不

最佳答案

As element is not appended in the DOM, you can not access it using $ selector. You can use html variable holding element in the form of string, content will not be the document but you can access #test element after wrapping it with $

 $.each(item, function(k, item2) {
var html = '<td><div id="test">' + item2.Name + '</div></td>';
$(html).find('#test').append("<div class='test class'></div>");
});

关于javascript - 将一个 div 附加到动态创建的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36348868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com