简体   繁体   English

单击一个按钮并在同一页面上显示javascript函数

[英]Clicking a button and displaying a javascript function on same page

I'm new to html and javascript. 我是html和javascript的新手。 How can I display the for loop that I have in my javascript function on the same page upon clicking the button. 单击按钮后,如何在同一页的javascript函数中显示我的for循环。 Right now I click the button and it brings me to a whole different page displaying 0 to -99. 现在,我单击该按钮,它将带我到显示0到-99的整个页面。 I want the numbers to appear below the button on the same page. 我希望数字显示在同一页面上的按钮下方。 Any ideas? 有任何想法吗?

         <fieldset>
         <legend>Loop Until -99</legend>
         <form name="loopuntil99" method="post">
         <input type="button" value="Display -99" onclick="displayArray()" />
         </form>
         </fieldset>

         </body>

         <script type="text/javascript">
       function displayArray()
       {

      for (i=0;i>-100;i--)
      {

    document.write(i + ", ");

       } 
        }

    </script>

     </html>

You can add a div and just modify the HTML in that. 您可以添加一个div并只需在其中修改HTML。

    <div id="output"></div>
</body>
<script type="text/javascript">
function displayArray()
{
    var div = document.getElementById("output");
    div.innerHTML = "0";
    for (i=-1;i>-100;i--)
    {
        div.innerHTML += ", " + i;
    } 
}
</script>

Looks like you are outside the <body> tag. 看起来您在<body>标记之外。 Place that code before the </body> tag and try again. 将该代码放在</body>标记之前,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM