本文介绍了有没有办法在documentFragment中找到一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var oFra = document.createDocumentFragment();
// oFra.[add elements];
document.createElement("div").id="myId";
oFra.getElementById("myId"); //not in FF
在将片段附加到文档之前,我该如何获取myId?
How can I get "myId" before attaching fragment to document?
推荐答案
如何:
var oFra = document.createDocumentFragment();
var myDiv = document.createElement("div");
myDiv.id="myId";
oFra.appendChild(myDiv);
oFra.getElementById("myId"); //not in FF
除非您添加了创建的 div
到你的文档片段我不知道为什么 getElementById
会找到它?
Unless you've added the the created div
to your document fragment I'm not sure why getElementById
would find it?
-edit
如果你愿意滚动自己的getElementById函数,那么你应该能够获得你以后的引用,因为这段代码的工作原理是: p>
If you're willing to roll your own getElementById function then you ought to be able to get the reference you're after, because this code works:
var oFra = document.createDocumentFragment();
var myDiv = document.createElement("div");
myDiv.id = "myId";
oFra.appendChild(myDiv);
if (oFra.hasChildNodes()) {
var i=0;
var myEl;
var children = oFra.childNodes;
for (var i = 0; i < children.length; i++) {
if (children[i].id == "myId") {
myEl = children[i];
}
}
}
window.alert(myEl.id);
这篇关于有没有办法在documentFragment中找到一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!