将片段附加到节点不工作在ie7,但在ie8中工作

Appending a fragment to a node is not working in ie 7 but working in ie8

本文关键字:工作 ie7 但在 ie8 片段 节点      更新时间:2023-09-26

我使用以下代码将一些ulli元素直接添加到DOM中。我正在使用一个片段,并将子节点附加到片段,最后将片段附加到节点。

function updateTree(objArray,leafClass){
    var currentElement = document.getElementById(currentNodeId);
    currentElement.setAttribute("class",'jstree-open jstree-unchecked');
    var fragment = document.createDocumentFragment();
    var ulElement = document.createElement('ul');
    var i = objArray.length;
    //LoopStart
    for (var i = 0, j = objArray.length ; i < j; i++ ){
    //while(i--){
    var liElement = document.createElement('li');
    liElement.setAttribute("id", objArray[i].attr.id);
    liElement.setAttribute("class",'jstree-closed jstree-unchecked '+leafClass+'');
    liElement.innerHTML = "<ins class='"jstree-icon'">&nbsp;</ins><a href='"#'" class='"'"><ins class='"jstree-checkbox'">&nbsp;</ins><ins class='"jstree-icon'">&nbsp;</ins>" + objArray[i].data + "</a> ";
    if(currentElement.getAttribute("class").indexOf('leaf') > 0){
        ulElement.appendChild(liElement);
        fragment.appendChild(ulElement);
        //$(currentElement.childNodes[1]).removeClass('jstree-laoding');
        //console.log("update Tree inside if: " + objArray[i].data);
        return "OK";
    }
    ulElement.appendChild(liElement);
    fragment.appendChild(ulElement);
    //console.log("update Tree for : " + objArray[i].data);
    //}
    }
    //LoopEnd
    currentElement.appendChild(fragment.cloneNode(true));
    currentElement.childNodes[1].setAttribute("class","");
    //console.log("update Tree end: ");
    ////console.log("time"+timer.getDiff());
    //$(currentElement.childNodes[1]).removeClass('jstree-laoding');
    return "OK";
  }

代码在IE8和Firefox中工作良好,但在IE7中,我无法看到我正在添加的元素;

我不确定,但我猜问题出在你的

if(currentElement.getAttribute("class").indexOf('leaf') > 0){
        ulElement.appendChild(liElement);
        fragment.appendChild(ulElement);
        //$(currentElement.childNodes[1]).removeClass('jstree-laoding');
        //console.log("update Tree inside if: " + objArray[i].data);
        return "OK";
    }

我试了一个简单的代码

<html><head><script>
function abc(){
alert(document.getElementById("a1").getAttribute("class"));
}
</script>
</head>
<body onload="abc();">
<div id="a1" class="ac"></div>
</body>
</html>

警报Ff -> acIe8 -> null-> null

解决方案:使用className

代替getAttribute
  • document.getElementById("a1").getAttribute("class") ->错误实施
  • . getelementbyid (" a1 ")。

if块的使用比较突兀,希望您能根据您的使用情况检查一下。