Javascript 如何使用按钮刷新浏览器页面?

Javascript 如何使用按钮刷新浏览器页面?,javascript,jquery,Javascript,Jquery,我一直在学习JavaScript,并且一直在开发一个可定制的随机数生成器。如果您决定要执行多个操作,则必须刷新它。我尝试过循环,但这使它无限大。 代码如下: <DOCTYPE! html> <body> <script> //This is a simple thingy, to randomly pick a number under a certain number //This is how we get what they wan

我一直在学习JavaScript,并且一直在开发一个可定制的随机数生成器。如果您决定要执行多个操作,则必须刷新它。我尝试过循环,但这使它无限大。 代码如下:

<DOCTYPE! html>
<body>
  <script>
    //This is a simple thingy, to randomly pick a number under a certain number

    //This is how we get what they want the max to be
    var input = prompt("Pick a number to be the maximum");

    //This is what it will output
    var output = Math.floor(Math.random() * (input + 1));
    //The reason for +1 is that it never reaches the max number without it

    //This is the output
    alert(output);
  </script>
</body>

//这是一个简单的方法,在某个数字下随机选取一个数字
//这就是我们如何得到他们想要的最大值
var输入=提示(“选择一个数字作为最大值”);
//这就是它将输出的内容
var输出=Math.floor(Math.random()*(输入+1));
//+1的原因是,如果没有它,它永远不会达到最大值
//这是输出
警报(输出);

假设您希望重新运行脚本而不是实际重新加载页面,您可以执行以下操作:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 
随机生成
函数随机化器(){
var输入=提示(“选择一个数字作为最大值”);
var输出=Math.floor(Math.random()*(输入+1));
警报(输出);
}

通过使用函数,您可以多次调用一段代码。

假设您希望重新运行脚本而不是实际重新加载页面,您可以执行以下操作:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 
随机生成
函数随机化器(){
var输入=提示(“选择一个数字作为最大值”);
var输出=Math.floor(Math.random()*(输入+1));
警报(输出);
}

通过使用函数,您可以多次调用一段代码。

假设您希望重新运行脚本而不是实际重新加载页面,您可以执行以下操作:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 
随机生成
函数随机化器(){
var输入=提示(“选择一个数字作为最大值”);
var输出=Math.floor(Math.random()*(输入+1));
警报(输出);
}

通过使用函数,您可以多次调用一段代码。

假设您希望重新运行脚本而不是实际重新加载页面,您可以执行以下操作:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 
随机生成
函数随机化器(){
var输入=提示(“选择一个数字作为最大值”);
var输出=Math.floor(Math.random()*(输入+1));
警报(输出);
}

通过使用函数,您可以多次调用一段代码。

您可以通过设置
window.location
属性,使用JavaScript刷新页面。要刷新,只需将其设置为自身:

window.location = window.location;
要在单击按钮时执行此操作,请在HTML中创建一个按钮:

<button id="refresh"></button>

通过设置
window.location
属性,可以使用JavaScript刷新页面。要刷新,只需将其设置为自身:

window.location = window.location;
要在单击按钮时执行此操作,请在HTML中创建一个按钮:

<button id="refresh"></button>

通过设置
window.location
属性,可以使用JavaScript刷新页面。要刷新,只需将其设置为自身:

window.location = window.location;
要在单击按钮时执行此操作,请在HTML中创建一个按钮:

<button id="refresh"></button>

通过设置
window.location
属性,可以使用JavaScript刷新页面。要刷新,只需将其设置为自身:

window.location = window.location;
要在单击按钮时执行此操作,请在HTML中创建一个按钮:

<button id="refresh"></button>
你可以做:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 

重新加载页面
函数myFunction(){
location.reload();
}

您可以:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 

重新加载页面
函数myFunction(){
location.reload();
}

您可以:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 

重新加载页面
函数myFunction(){
location.reload();
}

您可以:

<button onlick="randomizer()">Generate Random</button>
<script>
  function randomizer(){
    var input = prompt("Pick a number to be the maximum");
    var output = Math.floor(Math.random() * (input + 1));
    alert(output);
  }
</script>
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Reload page</button>

<script>
function myFunction() {
    location.reload();
}
</script>

</body>
</html> 

重新加载页面
函数myFunction(){
location.reload();
}

一些简单的重构可能会有所帮助,但回答最初的问题是如何刷新页面

也就是说,您可以使整个代码块成为一个可以多次调用的函数。就iut而言,它只执行一次,因为它没有设置为可重用。但是,请将其包装在函数中:

function randomNumber(){
  */ our code */
}
然后在页面加载时调用它(可以在函数声明之后):

然后(可选)可以将其绑定到按钮,以便再次调用。e、 g

<button onclick="randomNumber();">Re-run</button>
重新运行

一些简单的重构可能会有所帮助,但回答最初的问题是如何刷新页面

也就是说,您可以使整个代码块成为一个可以多次调用的函数。就iut而言,它只执行一次,因为它没有设置为可重用。但是,请将其包装在函数中:

function randomNumber(){
  */ our code */
}
然后在页面加载时调用它(可以在函数声明之后):

然后(可选)可以将其绑定到按钮,以便再次调用。e、 g

<button onclick="randomNumber();">Re-run</button>
重新运行

一些简单的重构可能会有所帮助,但回答最初的问题是如何刷新页面

也就是说,您可以使整个代码块成为一个可以多次调用的函数。就iut而言,它只执行一次,因为它没有设置为可重用。但是,请将其包装在函数中:

function randomNumber(){
  */ our code */
}
然后在页面加载时调用它(可以在函数声明之后):

然后(可选)可以将其绑定到按钮,以便再次调用。e、 g

<button onclick="randomNumber();">Re-run</button>
重新运行

一些简单的重构可能会有所帮助,但回答最初的问题是如何刷新页面

也就是说,您可以使整个代码块成为一个可以多次调用的函数。就iut而言,它只执行一次,因为它没有设置为可重用。但是,请将其包装在函数中:

function randomNumber(){
  */ our code */
}
然后在页面加载时调用它(可以在函数声明之后):

然后(可选)可以将其绑定到按钮,以便再次调用。e、 g

<button onclick="randomNumber();">Re-run</button>
重新运行

如果要使用循环,可以查看以下JSFIDLE:

我使用
确认
询问用户是否要继续。如果他们这样做,他们将选择OK,否则他们选择cancel,循环退出

以下是在小提琴中找到的代码:

var input; 
var contuine = true;
var output; 

while(contuine) {
    input = prompt("Pick a number to be the maximum");
    output = Math.floor(Math.random() * (input + 1)) 
    alert(output);
    contuine = confirm("Contuine?");
}

如果要使用循环,可以查看以下JSFIDLE:

我使用
确认
询问用户是否要继续。如果他们这样做,他们将选择OK,否则他们选择cancel,循环退出

以下是fidd中的代码