下面是我的代码,它在脚本运行时抛出错误。这是添加更多菜单的代码。当我运行脚本时,它会抛出错误
无法在'Node'上执行'appendChild':新的子元素包含父元素。
$(document).ready(function() {
ulNodes = document.getElementById("sub");
liNodes = [];
for (var i = 0; i < ulNodes.childNodes.length; i++) {
if (ulNodes.childNodes[i].nodeName == "LI") {
mpData = ulNodes.childNodes[i].getAttribute('class');
ulNodes.childNodes[i].setAttribute('class', 'n-menu' + " " + mpData);
liNodes.push(ulNodes.childNodes[i]);
}
}
var inp = document.getElementById("sub");
tot_Li = inp.getElementsByClassName("n-menu").length;
var n;
wWidth = window.innerWidth;
if ((wWidth > 767) && (wWidth < 800)) {
n = 4;
}
if ((wWidth > 800) && (wWidth < 1024)) {
n = 5;
}
if (wWidth > 1025) {
n = 7;
}
if (tot_Li > n) {
myMore();
}
function myMore() {
myUl = document.getElementById("sub");
myLi = document.createElement("LI");
myUl.appendChild(myLi);
myLi.setAttribute('id', 'more');
subMore = document.getElementById("more");
subAl = document.createElement("A");
subAl.setAttribute('href', '#nogo');
subAl.innerHTML = "MORE";
subMore.appendChild(subAl);
subUl = document.createElement("UL");
subMore.appendChild(subUl);
subUl.setAttribute('id', 'moreUl');
var mrUl = document.getElementById("moreUl");
var old_Li = document.getElementsByClassName("n-menu");
v = old_Li.length;
nw_Li = [];
for (var i = v; i > n; i--) //i=8 i>6
{
v = v - 1;
mrUl.appendChild(old_Li[v]);
}
}
});