为什么在浏览器中打开网页时不执行功能?

时间:2015-10-12 04:25:31

标签: javascript loops if-statement browser prompt

我正在尝试创建一个无限循环的函数,直到用户输入他们想要退出。但是,每次我在浏览器中打开网页时,都不会出现提示框,并且infiniteLoop()功能不会执行。为什么infiniteLoop()函数没有被调用?

function infiniteLoop() {
    i= 0;
    var begin= prompt("Shall we begin?");
    if (begin == "Yes") {
        var tryAgain= prompt("Exit loop?");
        if (tryAgain != "Yes") {
            infiniteLoop();
        }
    }
}

4 个答案:

答案 0 :(得分:2)

您需要在窗口onload事件中调用该函数。

window.onload = function(){
     infiniteLoop();
}

或者,如果你正在使用jquery

$(function() {
    infiniteLoop();
});

答案 1 :(得分:1)

提示框不会显示,因为您在网页加载时没有调用该功能。

声明您的功能后,添加 infiniteLoop()

e.g。

function infiniteLoop() {
    i= 0;
    var begin= prompt("Shall we begin?");
    if (begin == "Yes") {
        while (i < 5) {
            var tryAgain= prompt("Are you sure?");
            if (tryAgain == "Yes") {
                i++;
            }
            else {
                infiniteLoop();
            }
        }
    }
    else {
        infiniteLoop();
    }
}
// Initial call
infiniteLoop();

答案 2 :(得分:0)

IT工作,写5次&#34;是&#34;(区分大小写)它完成,否则它无限运行,并且不要忘记调用 infiniteLoop()

答案 3 :(得分:0)

清理并工作

<center><table width="1000" border="1" cellspacing="0"></center>
<form name="myForm">
  <tr height="150">
     <td bgcolor="#FF8080"colspan="2"><center><font size="100">Welcome to my Store</font></center></td>
  </tr>
  <tr height="200">
     <td width="220"><center><img src="brook.jpg" width="200" heigth="200"></center></td>
     <td bgcolor="#F5D69F">Item Name: Brook T-shirt <br><br>
     <input type="radio" name="radio1" id="small1" value="small1" checked>Small <br>
     <input type="radio" name="radio1" id="medium1" value="medium1">Medium <br>
     <input type="radio" name="radio1" id="large1" value="large1">Large <br>
     <br><br> Quantity:<input type="text" name="quantity1" id="quantity1" value="">
         <input type="button" id="button1" value="   BUY   " onClick="javascript: Clickme1();">
     Price:<input type="text" name="quantity" id="total1" value=""></td>
  </tr>

  <tr height="200">
     <td width="220"><center><img src="cross.jpg" width="200" heigth="200"></center></td>
     <td bgcolor="#F5D69F">Item Name: Brook T-shirt <br><br>
     <input type="radio" name="radio2" id="small2" value="small2" checked>Small <br>
     <input type="radio" name="radio2" id="medium2" value="medium2">Medium <br>
     <input type="radio" name="radio2" id="large2" value="large2">Large <br>
     <br><br> Quantity:<input type="text" name="quantity2" id="quantity2" value="">
         <input type="button" id="button1" value="   BUY   " onClick="javascript: Clickme2();">
     Price:<input type="text" name="quantity2" id="total2" value=""></td>
  </tr>

  <tr height="200">
     <td width="220"><center><img src="plain.jpg" width="200" heigth="200"></center></td>
     <td bgcolor="#F5D69F">Item Name: Plain T-shirt <br><br>
     <input type="radio" name="radio3" id="small3" value="small3" checked>Small <br>
     <input type="radio" name="radio3" id="medium3" value="medium3">Medium <br>
     <input type="radio" name="radio3" id="large3" value="large3">Large <br>
     <br><br> Quantity:<input type="text" name="quantity3" id="quantity3" value="">
         <input type="button" id="button1" value="   BUY   " onClick="javascript: Clickme3();">
     Price:<input type="text" name="quantity3" id="total3" value=""></td>
  </tr>

  <tr height="200">
     <td width="220"><center><img src="long.jpeg" width="200" heigth="200"></center></td>
     <td bgcolor="#F5D69F">Item Name: Long Sleeves <br><br>
     <input type="radio" name="radio4" id="small4" value="small4" checked>Small <br>
     <input type="radio" name="radio4" id="medium4" value="medium4">Medium <br>
     <input type="radio" name="radio4" id="large4" value="large4">Large <br>
     <br><br> Quantity:<input type="text" name="quantity4" id="quantity4" value="">
         <input type="button" id="button1" value="   BUY   " onClick="javascript: Clickme4();">
     Price:<input type="text" name="quantity4" id="total4" value=""></td>
  </tr>

  <tr height="200">
     <td width="220"><center><img src="polo.jpeg" width="200" heigth="200"></center></td>
     <td bgcolor="#F5D69F">Item Name: Polo Shirt <br><br>
     <input type="radio" name="radio5" id="small5" value="small5" checked>Small <br>
     <input type="radio" name="radio5" id="medium5" value="medium5">Medium <br>
     <input type="radio" name="radio5" id="large5" value="large5">Large <br>
     <br><br> Quantity:<input type="text" name="quantity5" id="quantity5" value="">
         <input type="button" id="button1" value="   BUY   " onClick="javascript: Clickme5();">
     Price:<input type="text" name="quantity5" id="total5" value=""></td>
  </tr>

  <tr>
    <td colspan="2" height="50" bgcolor="#CCFFB2">TOTALBILL:<input type="text" name="total" id="totalbill" value="" size="25"></td>
  </tr>

</form>
</table>
相关问题