我创建了这个createElement函数,它是有效的,因为它扩展了dom,但是为什么警报和console.log下面显示:“未定义”? 感觉就像异步执行。
const trial = createElement(body[0], 'h2', 'delay or not delay', null, null);
alert(trial);
console.log(trial, 'trial');
以及createElement函数:
function createElement(parent, tagname, textcontent, id, clas) {
let element = document.createElement(tagname);
if (textcontent) {
element.textContent = textcontent;
}
if (id) {
element.setAttribute('id', id);
}
if (clas) {
element.setAttribute('class', clas)
}
parent.appendChild(element)
}
最佳答案
您的 createElement
函数不返回任何内容。
关于javascript - 为什么我的正常同步代码线像异步一样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56043643/