gpt4 book ai didi

javascript - 当我更改 document.getElementById 时,Ajax 不起作用

转载 作者:行者123 更新时间:2023-11-28 15:16:43 25 4
gpt4 key购买 nike

我在 W3Schools 中找到了以下代码,当我使用 document.getElementById 时当我将其更改为 document.getElementsByClassName 时,它起作用了(在我的完整代码中,我将有超过 <p class="txthint"> 为什么我认为我应该使用 documents.getElementsByClassName )它只是停止工作。

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

Set Recordset1_cmd = Server.CreateObject ("ADODB.Command")
Recordset1_cmd.ActiveConnection = MM_PSCRM_STRING
Recordset1_cmd.CommandText = "SELECT prodref FROM dba.proditem where created >= '2015-08-01' and obsolete = '0' ORDER BY prodref asc"
Recordset1_cmd.Prepared = true

Set Recordset1 = Recordset1_cmd.Execute
Recordset1_numRows = 0
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form action="">
<input type="text" onChange="showCustomer(this.value)" value="">
</form>

<br>
<div class="txtHint">Customer info will be listed here...</div>

<script>
function showCustomer(str) {
var xhttp;
if (str == "") {
document.getElementsByClassName("txtHint").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementsByClassName("txtHint").innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", "data.asp?prodref="+str, true);
xhttp.send();
}
</script>

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

最佳答案

getElementsByClassName 返回节点的集合,而不是单个节点。您需要迭代该集合并在每个节点上设置 innerHTML 属性:

var nodes = document.getElementsByClassName("txtHint");
for (var i = 0; i < nodes.length; i++)
nodes[i].innerHTML = '';

您当前的代码正在设置集合本身的属性,因为它是完全有效的 JavaScript,所以不会出错,但也不会导致任何更新 - 只是现在节点集合有一个未使用的 innerHTML 属性(property)。

关于javascript - 当我更改 document.getElementById 时,Ajax 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33571964/

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