简体   繁体   English

如何重新提示用户(或循环提示),直到用户终止 javascript 中的程序?

[英]how to reprompt the user (or loop a prompt), until user terminates programs in javascript?

I am currently making a simple html page with javascript code where I have to prompt the user with a question to fill in a color-name which changes the background of the page after to the color of the user-input.我目前正在使用 javascript 代码制作一个简单的 html 页面,我必须提示用户填写一个颜色名称,该名称将页面的背景更改为用户输入的颜色。

If that's done, I'd like to continuously display the prompt on the page so that the user can keep entering color-names until he wishes to exit the page or terminate the program.如果这样做了,我想在页面上持续显示提示,以便用户可以继续输入颜色名称,直到他希望退出页面或终止程序。

It has to be written in the simplest/least amount of codelines as possible, and with the use of "prompt()" and "background.style.bgcolor".它必须用最简单/最少的代码行编写,并使用"prompt()" and "background.style.bgcolor".

Do you know how to do it?你知道怎么做吗?

<!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>

Prompt the user for a colour.提示用户输入颜色。 If the prompt returns a colour, change the background of the document body, and call the function again.如果提示返回颜色,请更改文档正文的背景,然后再次调用该函数。 If the prompt returns an empty string, or the user clicks cancel, don't call the function.如果提示返回空字符串,或者用户点击取消,则不要调用该函数。

 const body = document.body; function changeColor() { const color = prompt('Enter a color.'); if (color) { body.style.backgroundColor = color; changeColor(); } } changeColor();
 <h3>Background Color Changer</h3>

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

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