quot;getElementById not a functionquot; when trying to parse an AJAX response?(“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; }
然后我试图在 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
您可以通过递归遍历 children
来实现自己的函数来搜索 DOM.在较新的浏览器上,您甚至可以使用 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 响应时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“getElementById 不是函数"尝试解析 AJAX 响应时?


- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01