for 循环和innerHTML

Suc*_*nch 0 javascript innerhtml

如何使用innerHTML用段落填充一些文本?我需要在文本框中输入一个数字,然后重复一些文本与输入到文本框中的数字一样多的次数。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ex10.js</title>
</head>
<body>
    <input type="text" id="input-type" onKeyUp="myFunction()">
    <p id="sample-text">Here is some text</p>
    <script>
        function myFunction() {
            let count = document.getElementById("input-type").value;
            document.getElementById("sample-text").innerHTML = count;

            if (isNaN(count)) {
                document.getElementById("sample-text").innerHTML = "Error. Not a number";
            }
            else {
                for(int i = 0; i < count; i++) {
                    document.getElementById("sample-text").innerHTML = "This is some text";
                }

            }
        }
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Atm*_*ala 6

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ex10.js</title>
<script>
     function myFunction() {
        var count = document.getElementById("input-type").value;


        if (isNaN(count)) {
            document.getElementById("sample-text").innerHTML = "Error. Not a number";
        }
        else {
             // create your empty array
        var lines = "";

        // loop x amount of times and add your text to the array
        for (var i = 0; i < count; i++) {
            lines = lines+"Atmiya\n";
        }

        // convert the array to a string, with each string on its own line
        document.getElementById("sample-text").innerHTML = lines;
            }

        }
    </script>
</head>
<body>
    <input type="number" id="input-type" onKeyUp="myFunction()">
    <p id="sample-text">Here is some text</p>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)