gpt4 book ai didi

javascript返回对象htmlelement

转载 作者:行者123 更新时间:2023-12-02 16:48:50 25 4
gpt4 key购买 nike

我正在开发一个计算器应用程序。我创建了以下函数来将当前函数推送到 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/

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