6 回答
TA贡献1883条经验 获得超3个赞
因为运算从前往后的原因,first + second不先进行计算的话会被转换成字符串直接拼接的,所以需要提高优先级。
document.getElementById("add-btn").onclick = function() {
var first = Number(document.getElementById("first-number").value);
var second = Number(document.getElementById("second-number").value);
// 这句改了下计算优先级
document.getElementById("result").innerHTML = "运算结果" + (first + second);
};
TA贡献1844条经验 获得超8个赞
document.getElementById("add-btn").onclick = function(){
var first = document.getElementById("first-number").value;
var second = document.getElementById("second-number").value;
document.getElementById("result").innerHTML = ("运算结果:"+ (Number(first) + Number(second)));
}
TA贡献1793条经验 获得超6个赞
document.getElementById("add-btn").onclick = function() {
var first = Number(document.getElementById("first-number").value);
var second = Number(document.getElementById("second-number").value);
document.getElementById("result").innerHTML = `运算结果${first + second}`;
};
添加回答
举报