的getElementById临时文件是不是功能 [英] getElementById in temporary document is not function
问题描述
我有问题,当我想要得到的ID在li元素,并存储在临时文件。 这是我的javascript code。
i have problem when i want to get id in li element and stored in temporary document. this is my javascript code.
var tempNode = document.createElement("div"); //creating element for storing response
tempNode.innerHTML=ajaxhttp.responseText;
var nodes = tempNode.getElementsByTagName("div");
ajaxhttp.responseText等于
ajaxhttp.responseText equals to
<div id="side">
.....
parent
parent
</div>
</li>
<li id="recent-posts-3" class="widget widget_recent_entries"> <h2 class="widgettitle">fdfs</h2>
<ul>
<li><a href="example.com2011/11/16/43422/" title="fdsdfしい! モsdf モバイルdsf</a></li>
</ul>
</li>
<li id="text-7" class="widget widget_text">
......
</div>
我是索里如果太多,但是这是我的问题。 当我尝试循环并获得ID与此code
i'm sory if too much but that is my problem. when i try to loop and get id with this code
for (n=0; n<nodes.length; n++) {
console.log(tempNode.getElementById('recent-posts-3'));
}
的错误,当我尝试的document.getElementById('最近的桩-3')不function.and;
错误是一样的。我怎么能修复这个问题?
the error is not function.and when i try to document.getElementById('recent-posts-3');
the error is same. how i can fixed this problem?
请注意:
如果我tempNode仅此P>
if in my tempNode only this
<li id="recent-posts-3" class="widget widget_recent_entries"> <h2 class="widgettitle">fdfs</h2>
<ul>
<li><a href="example.com2011/11/16/43422/" title="fdsdfしい! モsdf モバイルdsf</a></li>
</ul>
</li>
这是工作。但不是在我的情况,我想所有的元素。 2.and我不是使用jQuery我想要纯JavaScript。
that's work. but not in my case i want to all element. 2.and i'm not use jquery i want pure javascript.
推荐答案
的getElementById是文档的方法,但你一个元素上使用它
getElementById is a method of document, but you use it on a element
您可以使用element.querySelector()代替:
You may use element.querySelector() instead:
tempNode.querySelector('li[id="recent-posts-3"]')
这篇关于的getElementById临时文件是不是功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!