简体   繁体   English

JavaScript document.body.style.backgroundColor 在 while 循环中不起作用

[英]JavaScript document.body.style.backgroundColor does not work in while loop

I'm using while loop to prompt user to enter for changing background-foreground color until user enters no.But the color does not change during run time, color only changes when user says no and loop breaks, not every time it is entered.我正在使用 while 循环提示用户输入更改背景 - 前景颜色,直到用户输入 no。但是颜色在运行时不会改变,颜色仅在用户说 no 和循环中断时改变,而不是每次输入时。 So only the color/value entered at last takes effect on page.所以只有最后输入的颜色/值才会在页面上生效。 When I run it in debug mode, it works fine.当我在调试模式下运行它时,它工作正常。

 function page_color() { while(1) { var input1=prompt("What is your favourite background color"); if (input1=='No' || input1=='no') break; document.body.style.backgroundColor = input1; var input2=prompt("What is your favourite foreground color"); if(input2=='No' || input2=='no') break; document.body.style.color = input2; } }
 <button onclick="page_color()">click here to change foreground and background color of page</button>

I don't know why you need a while loop inside the the page_colour function.我不知道为什么在 page_colour function 中需要一个 while 循环。

The following code seems to run just fine.以下代码似乎运行得很好。

<script>
  function page_color()
{

    var input1=prompt("What is your favourite background color"); 
    document.body.style.backgroundColor = input1;

    var input2=prompt("What is your favourite foreground color");
    document.body.style.color = input2;  

}
</script>
<button onclick="page_color()">click here to change foreground and background color of page</button>

Anyways, The answer to your question is given by W3schools.无论如何,您的问题的答案由 W3schools 给出。

https://www.w3schools.com/jsref/met_win_prompt.asp https://www.w3schools.com/jsref/met_win_prompt.asp

Do not overuse this method, as it prevents the user from accessing other parts of the page until the box is closed.不要过度使用此方法,因为它会阻止用户在关闭框之前访问页面的其他部分。 The prompt() method returns the input value if the user clicks "OK".如果用户单击“确定”,则 prompt() 方法返回输入值。 If the user clicks "cancel" the method returns null.如果用户单击“取消”,则该方法返回 null。

Since, the prompt and alert statements are blocking your application flow, developers do not use it.由于提示和警报语句阻塞了您的应用程序流程,因此开发人员不使用它。 (also because it looks ugly) (也因为它看起来很丑)

Alternative to prompt?提示的替代方案?

Use the input field to collect user input data and add an onClick event handler to run your page_colour function.使用input字段收集用户输入数据并添加 onClick 事件处理程序来运行您的 page_colour function。

Hope this helps.希望这可以帮助。

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

相关问题 document.body.style.backgroundColor的语法是什么? - What is the syntax for document.body.style.backgroundColor? document.body.style.backgroundColor未定义 - document.body.style.backgroundColor is undefined 为什么不工作 document.body.style.backgroundColor? - why not working document.body.style.backgroundColor? document.body.style.backgroundColor 在下一行之后执行 - document.body.style.backgroundColor getting executed after next line document.body.style.color = &#39;蓝色&#39;; document.body.style.fontSize = 18px&#39;; 在 Android WebKit 中不起作用 - document.body.style.color = 'blue'; document.body.style.fontSize = 18px'; does not work in Android WebKit HexColor 在 backgroundColor 中不起作用 - HexColor does not work in backgroundColor while循环内的JavaScript回调不起作用? - Javascript callback within a while loop does not work? Javascript:while循环不起作用 - Javascript : problem with while loop that does not work javascript 如何与 while(true)-loop 一起工作? - How does javascript work with while(true)-loop? Javascript 的“document.querySelector().style.backgroundColor”是否有理由将 HEX 转换为 RGB? - Is there a reason why Javascript's "document.querySelector().style.backgroundColor" converts HEX to RGB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM