为什么这个文本不更新?
Why is this text not updating?
我创建了一个简单的脚本,可以随机打开列表中的网站。我最近尝试添加一项功能,使用户能够选择每个页面打开和关闭之间的等待时间(以毫秒为单位)。这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Smokescreen</title>
</head>
<body align="center" style="font-family:monospace">
<h2 align="center" style="font-family:monospace">
SmokeScreen
</h2>
<h3 align="center" style="font-family:monospace">
SmokeScreen is a JavaScript program that opens a random site every 3 seconds,
</h3>
<h3 align="center" style="font-family:monospace">
to create a "smokescreen" over your browser history, preventing anyone from [easily] finding your information.
</h3>
<h4 align="center" style="font-family:monospace">To exit, close the original window that has the text "Smokescreen" in the title.</h4>
<p align="center" style="font-family:monospace">
<a href="github.com/keeganjk/smokescreen" style="font-family:monospace" align="center">Source Code / Learn More</a>
<br />
<div style="width: 300px; border: 25px solid #800000; margin:0 auto; font-family:monospace;" align="center">
<h4 align="center" style="font-family:monospace">Milliseconds to wait before closing page:</h4>
<h4 id="one" align="center" style="font-family:monospace"></h4>
<button onclick="millisecs = prompt('New time to wait (in milliseconds)?'); if ( isNaN(milliseconds) ) {millisecs = millisecsBackup} else {millisecsBackup = millisecs} document.getElementById('one').innerHTML = millisecs;" style="font-family:monospace" align="center">Set Wait Time</button>
<br />
<br />
</div>
<br />
<div style="margin:0 auto; font-family:monospace" align="center">
<button onclick="getRandom(0, list.length, millisecs);" style="font-family:monospace" align="center">Start!</button>
</div>
</p>
<script src="script.js"></script>
<script>
var millisecs = 3000;
var millisecsBackup = 3000;
document.getElementById("one").innerHTML = millisecs;
</script>
</body>
</html>
出于某种原因,当我输入不同的数字时,h4
中 id
或 one
中的文本不会更新。我做错了什么?
您正在检查 if (isNaN(milliseconds))
但您调用了您的变量 millisecs
所以它抛出了一个错误。
如果您使用的是现代浏览器,请尝试打开开发者工具(windows 上的 F12 / mac 上的 CMD+OPT+i)并检查控制台。
另外请考虑将您的内联样式/点击移动到 <style>
和 <script>
标签 :)
我创建了一个简单的脚本,可以随机打开列表中的网站。我最近尝试添加一项功能,使用户能够选择每个页面打开和关闭之间的等待时间(以毫秒为单位)。这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Smokescreen</title>
</head>
<body align="center" style="font-family:monospace">
<h2 align="center" style="font-family:monospace">
SmokeScreen
</h2>
<h3 align="center" style="font-family:monospace">
SmokeScreen is a JavaScript program that opens a random site every 3 seconds,
</h3>
<h3 align="center" style="font-family:monospace">
to create a "smokescreen" over your browser history, preventing anyone from [easily] finding your information.
</h3>
<h4 align="center" style="font-family:monospace">To exit, close the original window that has the text "Smokescreen" in the title.</h4>
<p align="center" style="font-family:monospace">
<a href="github.com/keeganjk/smokescreen" style="font-family:monospace" align="center">Source Code / Learn More</a>
<br />
<div style="width: 300px; border: 25px solid #800000; margin:0 auto; font-family:monospace;" align="center">
<h4 align="center" style="font-family:monospace">Milliseconds to wait before closing page:</h4>
<h4 id="one" align="center" style="font-family:monospace"></h4>
<button onclick="millisecs = prompt('New time to wait (in milliseconds)?'); if ( isNaN(milliseconds) ) {millisecs = millisecsBackup} else {millisecsBackup = millisecs} document.getElementById('one').innerHTML = millisecs;" style="font-family:monospace" align="center">Set Wait Time</button>
<br />
<br />
</div>
<br />
<div style="margin:0 auto; font-family:monospace" align="center">
<button onclick="getRandom(0, list.length, millisecs);" style="font-family:monospace" align="center">Start!</button>
</div>
</p>
<script src="script.js"></script>
<script>
var millisecs = 3000;
var millisecsBackup = 3000;
document.getElementById("one").innerHTML = millisecs;
</script>
</body>
</html>
出于某种原因,当我输入不同的数字时,h4
中 id
或 one
中的文本不会更新。我做错了什么?
您正在检查 if (isNaN(milliseconds))
但您调用了您的变量 millisecs
所以它抛出了一个错误。
如果您使用的是现代浏览器,请尝试打开开发者工具(windows 上的 F12 / mac 上的 CMD+OPT+i)并检查控制台。
另外请考虑将您的内联样式/点击移动到 <style>
和 <script>
标签 :)