<script type='text/javascript'>
var farhen;
var celesius;
var kelvin;
function FtoC() {
farhen = document.getElementById("F").value
var para = document.getElementById("R")
celesius = ((farhen - 32) / 1.8).toFixed(1);
para.innerHTML = farhen + " degress in farhenheit is " + celesius + " in centigrade" + "<br>";
}
function FtoK() {
farhen = document.getElementById("C").value
var para1 = document.getElementById("L")
kelvin = ((farhen - 32) / 1.8 + 273).toFixed(1);
para1.innerHTML = farhen + "degress in farhenheit is " + Kelvin + " in kelvins" + '<br>';
}
</script>
<h1>Farhenheit Convertor</h1>
<p>
Input degrees in Fahrenheit to convert to Celsius
<input type = "text" id = "F" />
<button type="button" Onclick="FtoC();">Click Me!</button>
</p>
<p id="R"></p>
<p>
Input degrees in Fahrenheit to convert to Kelvin
<input type = "text" id = "C" />
<button type="button" Onclick="FtoK();">Click Me!</button>
</p>
<p id="L"></p>
</body>
如何使第二个事件不起作用有人可以告诉我为什么它可能不起作用以及如何修复它?我正在尝试制作一个简单的转换器。
最佳答案
在 FtoK()
函数中连接输出字符串时,不应将 Kelvin
大写。应该是这样的:
function FtoK() {
farhen = document.getElementById("C").value
var para1 = document.getElementById("L")
kelvin = ((farhen - 32) / 1.8 + 273).toFixed(1);
para1.innerHTML = farhen + "degress in farhenheit is " + kelvin + " in kelvins" + '<br>';
}
关于javascript - 制作点击事件函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20727074/