DOM错误,用于在不存在的上下文中引用节点

时间:2013-11-05 11:30:39

标签: javascript html css dom

我尝试用DOM和javascript构建一个带有各种div的页面,如html:

<div id="divMain" class="divMain">
<div id="divT1" class="divT1"> onsite optimization </div>
<div id="divR1" class="divR1">
    <div id="divR11" class="divR11">img1</div>
    <a style="display:block" href="#">
        <div id="divR12" class="divR12">btn1</div>
    </a>
    <div id="divR13" class="divR13">btn2</div>
</div>
</div>

包含其他div的各种主要div。

我尝试在js脚本中使用DOM执行此操作:

oRmain = document.createElement("div");
oRmain.className = "divRMcss";
oRmain.id = "divRmain";


oRt1 = document.createElement("div");
oRt1.className = "divRt1css";
oRt1.id = "divRt1";


oRa1 = document.createElement("div");
oRa1.className = "divR1css";
oRa1.id = "divR1";
oRa11 = document.createElement("div");
oRa11.className = "divR11css";
oRa11.id = "divR11";
oRa12 = document.createElement("div");
oRa12.className = "divR12css";
oRa1.id = "divR12";
oRa13 = document.createElement("div");
oRa13.className = "divR13css";
oRa13.id = "divR13";


//Create on fly the entire page
    document.body.appendChild(oRmain);
    document.body.appendChild(oRt1);
    document.body.appendChild(oRa1);
    oMRch = document.getElementById("divRmain");
    oTRch = document.getElementById("divRt1");
    oAR1ch = document.getElementById("divR1");
    t = document.createTextNode("Onpage scan");
    //oOch = document.createTextNode(response.mtags.title);
    oTRch.appendChild(t);
    //oAR1ch.appendChild(oOch);
    oMRch.appendChild(oTRch);
    oMRch.appendChild(oAR1ch);

但是当我尝试此代码浏览器时会显示警告:

  

读取响应时出错:NotFoundError:尝试在不存在的上下文中引用节点。

我认为是“oMRch.appendChild(oAR1ch);(最后一行)创建错误,但为什么?

提前致谢 AM

1 个答案:

答案 0 :(得分:3)

可以使用DOM方法创建该结构,如下所示:

function createElement(tagName, id, className) {
    var elm = document.createElement(tagName);
    if (id) {
        elm.id = id;
    }
    if (className) {
        elm.className = className;
    }
    return elm;
}

var divMain = createElement('div', 'divMain', 'divMain');

var divT1 = createElement('div', 'divT1', 'divT1');
divT1.appendChild(document.createTextNode(" onsite optimization "));
divMain.appendChild(divT1);

var divR1 = createElement('div', 'divR1', 'divR1'); 
divMain.appendChild(divR1);

var divR11 = createElement('div', 'divR11', 'divR11');
divR11.appendChild(document.createTextNode('img1'));
divR1.appendChild(divR11);

var a = createElement('a');
a.style.display = "block";
a.href = "#";
divR1.appendChild(a);

var divR12 = createElement('div', 'divR12', 'divR12');
divR12.appendChild(document.createTextNode('btn1'));
a.appendChild(divR12);

var divR13 = createElement('div', 'divR13', 'divR13');
divR13.appendChild(document.createTextNode('btn2'));
divR1.appendChild(divR13);

document.body.appendChild(divMain);

或者您可以使用适用于所有浏览器的innerHTML

var divMain = document.createElement('div');
divMain.className = divMain.id = "divMain";
divMain.innerHTML =
    '<div id="divT1" class="divT1"> onsite optimization </div>' +
    '<div id="divR1" class="divR1">' + 
        '<div id="divR11" class="divR11">img1</div>' + 
        '<a style="display:block" href="#">' + 
            '<div id="divR12" class="divR12">btn1</div>' + 
        '</a>' + 
        '<div id="divR13" class="divR13">btn2</div>' + 
    '</div>';
document.body.appendChild(divMain);