【发布时间】:2021-11-07 17:26:40
【问题描述】:
我目前正在使用 javascript 代码制作一个简单的 html 页面,我必须提示用户填写一个颜色名称,该颜色名称会在用户输入的颜色之后更改页面的背景。
如果这样做了,我想在页面上持续显示提示,以便用户可以继续输入颜色名称,直到他希望退出页面或终止程序。
必须尽可能以最简单/最少的代码行编写,并使用"prompt()" and "background.style.bgcolor".
你知道怎么做吗?
<!DOCTYPE html>
<html lang="en">
<title> Background Colors</title>
<body onload="changeColor()" ontimeupdate="userInput" load="3sec">
<h1> <b style="text-align: center;">Background Color Changer</b> </h1>
<form name = "myform">
</form>
</body>
<script type="text/javascript" style="align-items: center; justify-content: center">
function changeColor()
{
var userInput = window.prompt("Enter a background-color:");
document.bgColor = userInput;
}
</script>
</html>
【问题讨论】:
-
听起来用户体验很棒!顺便说一句:脚本标签不会有
style属性 -
这个“我想在页面上连续显示提示”是不可能的。在按下 OK 按钮之前,您无法检索输入颜色。浏览器自动关闭提示。那时。您每次都必须重新打开提示。
标签: javascript html prompt