2 回答
TA贡献1820条经验 获得超9个赞
您定义并填充几个局部变量,然后将它们全部丢弃而不显示任何内容。你可能想要一个return声明:
function loop() {
var asterisk = ["*", "**", "***", "****", "*****", "******", "*******", "********"];
var text = "";
var i;
for (i = 0; i < asterisk.length; i++) {
text += asterisk[i] + "<br>";
}
return text; // <------------
}
document.querySelector("section").innerHTML = loop();
<section></section>
TA贡献1871条经验 获得超8个赞
text 的值正在正确生成。但是,您需要在 html 中的某处使用文本
function loop() {
var asterisk = ["*", "**", "***", "****", "*****", "******", "*******", "********"];
var text = "";
var i;
for (i = 0; i < asterisk.length; i++) {
text += asterisk[i] + "<br>";
}
document.getElementById("demo").innerHTML=text;
}
loop()
HTML
<html>
<head>
<title>Conditional statements and Loops</title>
</head>
<body>
<p id="demo"></p>
<script type="text/javascript" src="pattern_javascript.js">
</script>
</body>
</html>
参考:https ://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_document_getelementbyid
添加回答
举报
