我有这个脚本的问题。起初我对<div>
s 进行了硬编码,它工作正常。但是后来我需要动态更改导航栏,因为我更改了(数量)链接。
问题是 id 在注入element
.innerHTML = (html 字符串) 时没有注册。
我怎样才能解决这个问题?
这是代码:
javascript:
function constructNav() {
var submenu_layer_innerHTML = "";
var top_nav_innerHTML = "<UL class=\"nav_category\">";
for (i=0; i<links.length; i++) {
submenu_layer_innerHTML = "<div class=\"submenu\" id=\"submenu" + i + "\" onMouseOut=\"timeHideNav('" + i + "','1000')\"> </div>";
top_nav_innerHTML += "<LI id=\"link" + i + "\"><a href=# onMouseOver=\"expandNav('" + i + "');\">" + links[i] + "</a></LI>";
}
top_nav_innerHTML += "</UL>";
document.getElementById("top_nav").innerHTML = top_nav_innerHTML;
document.getElementById("submenu_layer").innerHTML = submenu_layer_innerHTML;
}
html:
<script type="text/javascript" src="scripts.js"> </script>
<script type="text/javascript">
var links =["link 1",
"link 2",
"link 3",
"link 4",
"link 5",
"link 6"];
</script>
<body onLoad="constructNav();">
<div id="submenu_layer"> </div>
<div id="top_nav"> </div>
我在 Firefox 中收到此错误:
line 35: document.getElementById("submenu"+pageNo) is NULL