简体   繁体   English

Javascript appendChild 两次向 DOM 添加一个元素?

[英]Javascript appendChild adds an element to the DOM twice?

I've got this code (TH is my table header DOM element):我有这个代码(TH 是我的表头 DOM 元素):

var typeIndicator = document.createElement('span');
typeIndicator.innerText = "A";
TH.appendChild(typeIndicator);

What happens is it appends 2 <span> elements instead of one.发生的情况是它附加了 2 个<span>元素而不是一个。 Anyone had this problem before?以前有人遇到过这个问题吗?

Most of the time double or multiple events triggering means that somewhere in your code you are assigning the same event listener more than once.大多数情况下,双重或多个事件触发意味着您在代码中的某处多次分配相同的事件侦听器。

First check your loops (if you have any), then I can advise you to put a debugger;首先检查你的循环(如果你有的话),然后我可以建议你放一个debugger; statement just above your snippet and debug the program flow using the browser's developer tools.语句,并使用浏览器的开发人员工具调试程序流。

PS: I would have commented instead of answering, if I only had enough reputation. PS:如果我只有足够的声誉,我会发表评论而不是回答。 If you could please add some context to your snippet (like some more enclosing code or even try reproducing your issue in a jsFiddle ) I may be able to expand my answer.如果您可以在您的代码段中添加一些上下文(例如一些更封闭的代码,甚至尝试在jsFiddle 中重现您的问题),我可以扩展我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM