我尝试运行以下代码,结果显示了一个空白屏幕
function make() {
for (var i = 1; i <= 8; i++) {
var j = "d" + i;
var c = document.createElement("div");
c.setAttribute("id",j);
document.getElementById(j).innerHTML = 'Hello<br>';
}
}
#d1 {font-family: 'Cinzel';}
#d2 {font-family: 'Cookie';}
#d3 {font-family: 'Great Vibes';}
#d4 {font-family: 'Monoton';}
#d5 {font-family: 'Orbitron';}
#d6 {font-family: 'Pacifico';}
#d7 {font-family: 'Righteous';}
#d8 {font-family: 'Sacramento';}
<!DOCTYPE html>
<html>
<head>
<title>Logo</title>
<link href="https://fonts.googleapis.com/css?family=Cinzel|Cookie|Great+Vibes|Monoton|Orbitron|Pacifico|Righteous|Sacramento" rel="stylesheet">
</head>
<body onload="make()">
</body>
</html>
在上面的代码片段中,我使用了一个javascript函数,其中我创建了8个元素,并用换行符分隔每个元素。但是,不幸的是,包含'innerHTML‘的行抛出了一个类型错误,并且代码的其余部分不会生成所需的输出。
请一定要帮帮我!
谢谢
发布于 2018-10-15 00:10:19
而不是那一行:
document.getElementById(j).innerHTML = 'Hello<br>';
试试这个:
c.textContent = 'Hello';
以及作为注释和答案-您应该使用带有appendChild (C)的appendChild将元素插入到文档树中;
https://stackoverflow.com/questions/52804576
复制相似问题