weixin_33712881 2015-01-09 18:53 采纳率: 0%
浏览 55

AJAX getElementsByClassName

I'm new to AJAX and have some problems understanding it. I have this code:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var titles = xmlhttp.responseText.getElementsByClassName('title');
        document.getElementById("mydiv").innerHTML=titles;
    }
}
xmlhttp.open("GET", "index.html", true);
xmlhttp.send();

This will produce the error: "Uncaught TypeError: undefined is not a function".

I don't really understand why I can't get the classes, id's or anything from it. How can I do this?

  • 写回答

1条回答 默认 最新

  • weixin_33733810 2015-01-09 18:55
    关注

    responseText will be a string, not a DOM object.

    Use responseXML instead.

    Note that getElementsByClassName will return a NodeList, not a string of HTML, so you'll also need to process it before assigning it to innerHTML.

    评论

    报告相同问题?

    悬赏问题

    • ¥15 如何在EXCEL某单元格中查找是否存在另一列中的内容。
    • ¥50 非对称加密(相关搜索:密码学)
    • ¥15 关于非编程寻迹小车的分析
    • ¥15 java导出EXCEL这里已经执行成功了,但是流浪器没有下载
    • ¥15 帮我把代码改的能正常运行就行
    • ¥50 有限元修正、模型修正、最小二乘法,详细步骤
    • ¥15 用plc编写梯形程序
    • ¥15 关于#物联网#的问题:新大陆AIOT中,按照手册教程进行添加,设置完成后网关一直不上线,显示Never这是网络服务的信息
    • ¥15 这个要用一维热方程但是我不知道怎么运用这个公式
    • ¥15 OpenFOAM多孔介质传热模型建模