gpt4 book ai didi

JavaScript 无法找到该元素

转载 作者:行者123 更新时间:2023-11-28 13:39:36 26 4
gpt4 key购买 nike

这是我的问题:

当我说 getelementbyid("table1") 时,它说“Uncaught TypeError: Cannot set property 'innerHTML' of null

我正在通过java脚本渲染动态表,如下所示:

function mainProcessing()
{
<% ProductController pc=new ProductController();%>

var val = <%=pc.doTask()%>
var jobj=JSON.stringify(val);
document.write(jobj);
alert(jobj);

var obj=JSON.parse(jobj);
alert("jobj");
alert(obj.toString());
var object = eval("(" + jobj+ ")");

alert("this part is done");
return object;
}
function drawtable()
{
var JSONObj=mainProcessing();
var tablecontents = "";
for (var i = 0; i < 5; i ++)
{
tablecontents += "<tr>";
tablecontents += "<td>" + i + "</td>";
tablecontents += "<td>" + i * 100 + "</td>";
tablecontents += "<td>" + i * 1000 + "</td>";
tablecontents += "</tr>";
}

document.write(JSONObj.toString());
alert("just outside nested loop");
document.getElementById("table1").innerHTML = tablecontents;
}

为了测试,我在表中插入了随机值。

html 部分如下所示:

<title>Indian Divine !!!</title>
</head>
<body onload="drawtable()">
<center>
<h1>my name is jobj</h1>
<table id="table1">

</table>
</center>
</body>
</html>

浏览器用户是 Chrome。IDE Eclips Juno

最佳答案

您不能使用 document.write 页面加载后。它取代了内容。如果您想查看其中的内容,请使用 console.log(jobj);

其次,如果您打算在 IE 中使用此代码,you can't set the tables innerHTML .

第三,不要使用eval()来转换 JSON。使用 JSON.parse()

关于JavaScript 无法找到该元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18741143/

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