gpt4 book ai didi

javascript - 错误 - 未捕获类型错误 : Cannot set property 'innerHTML' of undefined

转载 作者:行者123 更新时间:2023-12-02 15:26:36 24 4
gpt4 key购买 nike

我的代码中不断出现此错误。

未捕获的类型错误:无法设置未定义的属性“innerHTML”我读过一些其他具有类似错误的帖子未捕获的类型错误:无法设置未定义的属性“innerHTML”为null 我已经尝试了一些建议的方法,但似乎没有任何改变。

它似乎指向这一行 document.getElementsByClassName("demo")[i].innerHTML = xhttp.responseText; 我不明白为什么像我添加循环之前一样,它起作用了很好。

我的代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="../Connections/DVerto.asp" -->
<%
Dim Recordset1
Dim Recordset1_cmd
Dim Recordset1_numRows

Set Recordset1_cmd = Server.CreateObject ("ADODB.Command")
Recordset1_cmd.ActiveConnection = MM_DVerto_STRING
Recordset1_cmd.CommandText = "SELECT Part_Number FROM dbo.Stock_Header WHERE Part_Number like '84%'"
Recordset1_cmd.Prepared = true

Set Recordset1 = Recordset1_cmd.Execute
Recordset1_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = 10
Repeat1__index = 0
Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body onLoad="loadDoc()">
<table width="50%" border="0" cellspacing="2" cellpadding="2">
<%
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
%>
<tr>
<td class="prodref"><%=(Recordset1.Fields.Item("Part_Number").Value)%></td>
<td class="demo">&nbsp;</td>
</tr>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
Recordset1.MoveNext()
Wend
%>
</table>
<script>
var a = document.getElementsByClassName("prodref").length;
var i = 0;
for (i; i < a; i++) {
loadDoc();
}
function loadDoc() {
var a = document.getElementsByClassName("prodref");
a[i] = document.getElementsByClassName("prodref").innerHTML;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementsByClassName("demo")[i].innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "data.asp?prodref="+a[i].innerHTML, true);
xhttp.send();
}
</script>

</body>
</html>
<%
Recordset1.Close()
Set Recordset1 = Nothing
%>

最佳答案

您需要将 i 传递给该函数,因为循环在 AJAX 请求返回时已完成。

var prodref = document.getElementsByClassName("prodref");
var demo = document.getElementsByClassName("demo");
var i = 0;
var a = prodref.length;

for (i; i < a; i++) {
loadDoc(i);
}

function loadDoc(i) {
console.log("creating loadDoc() call for", i);

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {
console.log("callback invoked for", i, "with state:", xhttp.readyState);

if (xhttp.readyState == 4 && xhttp.status == 200) {
console.log("setting .innerHTML for", i, "to:", xhttp.responseText);
demo[i].innerHTML = xhttp.responseText;
}
};

xhttp.open("GET", "data.asp?prodref="+prodref[i].innerHTML, true);
xhttp.send();
}

现在,每个单独的 loadDoc 调用都有一个本地 i 变量,您在该函数中创建的回调函数将关闭该本地 i >.

这是一个闭包

我还缓存了您的 DOM 选择。您不需要一遍又一遍地获取节点,除非您认为它们会发生变化。

关于javascript - 错误 - 未捕获类型错误 : Cannot set property 'innerHTML' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33658611/

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