“getElementById不是函数”在尝试解析AJAX响应时? [英] "getElementById not a function" when trying to parse an AJAX response?

查看:174
本文介绍了“getElementById不是函数”在尝试解析AJAX响应时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 GM_xmlhttpRequest (在Greasemonkey脚本中)并将 responseText 存储到新创建的HTML元素中:

I'm running GM_xmlhttpRequest (in a Greasemonkey script) and storing the responseText into a newly created HTML element:

var responseHTML = document.createElement('HTML');
...
onload: function() { responseHTML.innerHTML = response.responseText; }



然后我试图在<$ c中找到一个元素$ c> responseHTML :

console.log(responseHTML.getElementsByTagName('div'));
console.log(responseHTML.getElementById('result_0'));



第一种工作正常,但不是第二种。有什么想法?


The first works fine, but not the second. Any ideas?

推荐答案

getElementById 不是HTML元素的方法。它是文档节点的一种方法。因此你不能这样做:

getElementById is not a method of HTML elements. It is a method of the document node. As such you can't do:

div.getElementById('foo'); // invalid code

您可以通过递归遍历<$ c来实现自己的函数来搜索DOM $ C>孩子。在较新的浏览器上,您甚至可以使用 querySelector 方法。对于最小的开发,您可以使用jQuery或sizzle.js(jQuery背后的查询引擎)等库。

You can implement your own function to search the DOM by recursively going through children. On newer browsers you can even use the querySelector method. For minimal development you can use libraries like jQuery or sizzle.js (the query engine behind jQuery).

这篇关于“getElementById不是函数”在尝试解析AJAX响应时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆