是否可以在动态创建的元素上使用querySelector()? [英] Is it possible to use querySelector() on a dynamically created element?

查看:429
本文介绍了是否可以在动态创建的元素上使用querySelector()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在循环中使用appendHtml()来动态添加li元素(在字符串中具有唯一的ID)到ul。当我需要通过他们的ID访问这些动态创建的li元素,例如,创建嵌套列表,querySelector()返回null。如果我应该能够做到这一点,那么在我的代码中可能会有错误。但是我想确认是这样的。

I'm using appendHtml() in a loop to dynamically add li elements (with unique IDs in the string) to a ul. When I need to access these dynamically created li elements by their IDs to, for example, create nested lists, querySelector() returns null. If I should be able to do this, then there could very well be bugs in my code. But I want to make sure this is the case.

推荐答案

元素如何添加并不重要,的DOM querySelector 可以找到它们。

It doesn't matter how elements are added, if the are part of the DOM querySelector can find them.

这篇关于是否可以在动态创建的元素上使用querySelector()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆