【问题标题】:Tried writing a do-while loop statement that prints out my statement, but it's not currently printing it尝试编写一个 do-while 循环语句来打印我的语句,但它当前没有打印它
【发布时间】:2015-09-15 19:09:23
【问题描述】:

所以,我正在尝试编写一个 do-while 循环语句来打印我的循环语句,但我的浏览器没有打印我的文本。我该如何编辑我的代码?

<!DOCTYPE html>
<html>
<head>
    <title>9. Looping Statements in Javascript</title>
</head>
<body>
    <h1>9. Looping Statements in Javascript</h1>
    <div id="playlist"></div>
    <div id="someResult"></div>
    <script>
        var playlist = [];
        playlist[0] = "Willy Wesly";
        playlist[1] = "Childish Gambino";
        playlist[2] = "Chance The Rapper";
        playlist[3] = "Travi$ Scott";
        playlist[4] = "Yeezy";

// while
    /*
    var i = 0;
    while (i < playlist.length) {
        var element = document.createElement('div');
        element.innerHTML = "Now Playing:  " + playlist[i];
         i++;
        document.body.appendChild(element);
}
*/

// do - while
var someResult = false;
do {
    var element = document.createElement('div');
    element.innerHTML = 'Will print AT LEAST once!';
}
while(someResult);
    </script>
</body>
</html>

【问题讨论】:

    标签: javascript html loops do-while


    【解决方案1】:

    您需要将元素添加到文档正文中。

    var someResult = false;
    do {
        var element = document.createElement('div');
        element.innerHTML = 'Will print AT LEAST once!';
    
        //You need to add it to the body for it to show on the page
        document.body.appendChild( element );
    }
    while(someResult);
    

    JSFiddle 显示这个:http://jsfiddle.net/azgxh59q/

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2015-12-11
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      相关资源
      最近更新 更多