我正在开发一个计算器应用程序。我创建了以下函数来将当前函数推送到 div 以显示自身。
function addtoScreen(vTag){
var screen = document.getElementById("screen");
if(currentFunction.length == 1){
var newCalc = document.createElement("p");
newCalc.setAttribute("class", "calc");
var opInt = "<span>"+vTag+"</span>";
newCalc.innerHTML = opInt;
screen.innerHTML = newCalc;
}else{
var opInt = "<span>"+vTag+"</span>";
newCalc = document.getElementById("screen").lastElementChild;
newCalc.innerHTML = opInt;
}
}
if 部分不断返回 [object HTMLParagraphElement] 我做错了什么?我尝试使用 .value 但只返回 null,因为该对象是没有 value 属性的整个元素。
编辑:我没有提到 vTag 只是从推送到数组的函数传递的字符串。
最佳答案
这样的事情怎么样(我注释掉了你的旧代码):
function addtoScreen(vTag){
var screen = document.getElementById("screen");
if(currentFunction.length == 1){
var newCalc = document.createElement("p");
//newCalc.setAttribute("class", "calc");
newCalc.className = "calc";
//var opInt = "<span>"+vTag+"</span>";
var opInt = document.createElement("span");
opInt.innerHTML = vTag;
//newCalc.innerHTML = opInt;
newCalc.appendChild(opInt);
//screen.innerHTML = newCalc;
screen.appendChild(newCalc);
}else{
//var opInt = "<span>"+vTag+"</span>";
var opInt = document.createElement("span");
opInt.innerHTML = vTag;
newCalc = screen.lastChild;
//newCalc.innerHTML = opInt;
if(newCalc)
{
newCalc.appendChild(opInt);
}
else
{
screen.appendChild(opInt);
}
}
}
关于javascript返回对象htmlelement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26869913/