gpt4 book ai didi

javascript - 如何使用JavaScript中的函数转换温度

转载 作者:行者123 更新时间:2023-12-01 01:06:56 25 4
gpt4 key购买 nike

我想构建代码,通过使用 Java 脚本中的任何函数将温度从摄氏温度转换为华氏温度我想知道如何从 window.prompt 中获取值并使用它通过 JavaScript 中的任何函数进一步转换温度?

我创建了 HTML,现在在 JavaScript 中我编写了 window.prompt,我可以在其中放置值。但我不知道如何从 window.prompt 中获取这个值并使用函数将其转换为华氏度。

var button = document.getElementById('greeter-button');
button.addEventListener('click', function() {
Temperature = window.prompt('What is temperature in celcius?');
});
<div id="button_and_text">
<button id="greeter-button">Button!</button>
<br> Click the button! Check the temperature!
<br><br>
</div>

<div id="greeter-output"></div>

最佳答案

你是说这个吗?请注意,您通常需要将从提示返回的字符串转换为数字,例如使用+温度,但乘法会将字符串转换为数字。运算符优先级在这里也有帮助

window.addEventListener("load", function() { // on page load
var button = document.getElementById('greeter-button'),
output = document.getElementById('greeter-output');
button.addEventListener('click', function() {
var temperature = window.prompt('What is temperature in celcius?');
if (temperature != null) {
output.innerHTML = temperature + "&deg;F = " +
(temperature * 9 / 5 + 32) + "&deg;C";
}
});
});
<div id="button_and_text">
<button id="greeter-button">Button!</button><br/>
Click the button! Check the temperature!
</div>

<div id="greeter-output"></div>

关于javascript - 如何使用JavaScript中的函数转换温度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com