开发者

documentFragement

please take a look at the following code.

  var oFra = document.createDocumentFragment();
    var myDiv = document.createElement("div");
    myDiv.id="myId";
    oFra.appendChild(myDiv);
    oFra.getElementById("myId");

In this case do i have ref to the div i just inserted inside documentFragement using the variable myDiv? Lets say i move ahead and add this 开发者_运维问答documentFragement to the actual DOM. Will I still be able to access the div with id="myId" using this "myDiv" variable???


If you try this, it works: http://www.jsfiddle.net/dactivo/4BSaF/

The problem is that you cannot use "oFra" + getElementById directly, once you append the fragment, you can access the div "myId" in the DOM.

  <div id="test"></div>
<script type="text/javascript">
     var oFra = document.createDocumentFragment();
        var myDiv = document.createElement("div");
        myDiv.id="myId";
    myDiv.innerHTML="hola";
        oFra.appendChild(myDiv);
       // oFra.getElementById("myId");


    document.getElementById("test").appendChild(oFra);

    alert(document.getElementById("myId").innerHTML);

</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜