防止显示的文本在几秒钟后隐藏

时间:2019-01-06 15:52:31

标签: javascript html html5 bottle

这里是我要运行的少量代码,问题是它只是显示一秒钟而不是永久显示。我说的是

<p id="demo"></p>

我该如何解决?

 function myFunction() {
        var s = document.getElementById("myText").value;

        var password = ""
        for (var i = 1; i <= s; i++) {
            var firstletter = Math.floor(Math.random() * 10);
            password = password + "" + firstletter;
        }
        document.getElementById("demo").innerHTML = "your password is:" + password;
    }  
<h1>how many numbers</h1>
    <input type="text" id="myText" value="">
    
      
    <button onclick="myFunction()">Try it</button>
    
   
    <p id="demo"></p>

1 个答案:

答案 0 :(得分:0)

您需要提供字符串的长度但是。 循环应为(var i = 1; i <= s.length; i++)

<h1>how many numbers</h1>
<input type="text" id="myText" value="">


<button onclick="myFunction()">Try it</button>

<script type="text/javascript">

    function myFunction() {
        var s = document.getElementById("myText").value;

        var password = ""
        for (var i = 1; i <= s.length; i++) {
            var firstletter = Math.floor(Math.random() * 10);
            password = password + "" + firstletter;
        }
        document.getElementById("demo").innerHTML = "your password is:" + password;
    }     
</script>
<p id="demo"></p>