更新时间:2023-02-25 15:05:14
如何:
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);