使用 javascript 查找动态添加的标签
我正在尝试了解如何找到在表中动态添加的标签,如下所示:
ParentTag= document.getElementById('parentdiv');
var newTag = document.createElement('div');
newTag.innerHTML="<span class="ImNew"></span>"
ParentTag.appendChild(newTag);
How will I be able to find that tag in javascript, not倾向使用jquery,所以没有live 请提出建议。尝试在严格的 javascript 中找到该新标签。
我指的标签是span标签
I am trying to see how to find a tag that was added dynamically within a table as so:
ParentTag= document.getElementById('parentdiv');
var newTag = document.createElement('div');
newTag.innerHTML="<span class="ImNew"></span>"
ParentTag.appendChild(newTag);
How will I be able to find that tag in javascript, not leaning towards using jquery so no live recommendations please.. Trying to find that new tag in strictly javascript.
The tag I am referring to is the span tag
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在创建新标签时为其指定一个 ID:
然后,只需使用该 ID 即可找到它:
You could give your new tag an ID when you create it:
Then later, just use that ID to find it:
这取决于元素中还存在哪些其他元素。例如,您可以获取元素中的所有 span 标记,并过滤掉具有特定类名的标记:
如果元素中只有一个具有该类名的 span 标记,则
filtered
数组现在包含一个对该元素的引用。That depends on what other elements exist in the element. You can for example get all span tags in the element and filter out the ones with a specific class name:
If there is only one span tag with that class name in the element, the
filtered
array now cotnains a reference to that element.正如其他人所提到的,如果您稍后需要它,为什么不直接跟踪它或给它一个
id
呢?但无论如何,这就是您可以进行手动搜索的方式(在将新项目添加到末尾时从后到前进行搜索)。
As others have mentioned, why not just keep track of it or give it an
id
if you're going to need it later?But anyway, this is how you could do a manual search (searching from back to front as you're adding the new items to the end).