我是JavaScript的初学者。我正试图让这个温度转换器工作,但对需要调用的东西感到困惑。为什么这段代码不起作用?
var convertCtoF = document.getElementById("degC");
convertCtoF.onchange = function(degC){
var degreesC = document.getElementById("degC").value;
var degreesF = (("degC" * 9/5)+ (32));
document.getElementById("degFOut").innerHTML = degreesF;
}
var convertFtoC = document.getElementById("degF");
convertFtoC.onchange = function(degF){
var degreesF = document.getElementById("degF").value;
var degreesC = (("degF" - 32) * (5/9));
document.getElementById("degCOut").innerHTML = degreesC;
}
答案 0 :(得分:1)
var degreesF = ((degreesC * 9/5)+ (32));
...
var degreesC = ((degreesF - 32) * (5/9));
答案 1 :(得分:0)
需要:
var convertCtoF = document.getElementById("degC");
convertCtoF.onchange = function(degC){
var degreesC = document.getElementById("degC").value;
var degreesF = ((degreesC * 9/5)+ (32));
document.getElementById("degFOut").innerHTML = degreesF;
}
var convertFtoC = document.getElementById("degF");
convertFtoC.onchange = function(degF){
var degreesF = document.getElementById("degF").value;
var degreesC = ((degreesF - 32) * (5/9));
document.getElementById("degCOut").innerHTML = degreesC;
}
您正在引用该变量,而不是字符串。
答案 2 :(得分:0)