我正在尝试用 DOM 和 javascript 在 html 中构建一个带有各种 div 的页面:
<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);(最后一行)会产生错误,但为什么呢?
提前致谢