简体   繁体   中英

Javascript appendChild adds an element to the DOM twice?

I've got this code (TH is my table header DOM element):

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

What happens is it appends 2 <span> elements instead of one. 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; 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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