我有以下代码在W3Schools上找到,当我使用document.getElementById时,它可以工作,当我将其更改为document.getElementsByClassName时(在我的完整代码中,除了我想使用)就停止工作。<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!--#include file="Connections/PSCRM.asp" --><%Dim Recordset1Dim Recordset1_cmdDim Recordset1_numRowsSet Recordset1_cmd = Server.CreateObject ("ADODB.Command")Recordset1_cmd.ActiveConnection = MM_PSCRM_STRINGRecordset1_cmd.CommandText = "SELECT prodref FROM dba.proditem where created >= '2015-08-01' and obsolete = '0' ORDER BY prodref asc"Recordset1_cmd.Prepared = trueSet Recordset1 = Recordset1_cmd.ExecuteRecordset1_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的文档,则会注意到您正在返回对象数组,而getElementById返回单个元素。对于数组,没有innerHtml的原型,它仅在单个元素上公开。您需要做的是遍历从getElementsByClassName中检索到的元素列表。var elements =document.getElementsByClassName("txtHint");for(var i = 0; i < elements.length; i++){ elements[i].innerHTML = xhttp.responseText};尝试一下,看看是否有帮助关于javascript - 更改document.getElementById时Ajax不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33571964/ 10-11 23:49