这个javascript滑动菜单不起作用 - 调试器给出的唯一错误是第一个IF语句是一个块命令&应该有花括号,我不明白。
有没有人看到明显的错误?我不确定我是否正确使用'this'关键字(以及函数showSlide中的变量slideList ...请帮助&我对格式化(白色空间)的任何反馈也很受欢迎!
window.onload = makeMenus;
var currentSlide = null;
var timeID = null;
var leftPos = 0;
function makeMenus() {
var slideMenus = new Array();
var allElems = document.getElementsByTagName("*");
for (var i = 0; i < allElems.length; i++) {
if (allElems[i].className == "slideMenus") menus.push(allElems[i]);
}
for (var i = 0; i < slideMenus.length; i++) {
slideMenus[i].onclick = showSlide;
slideMenus[i].ul.style.left = "0px"; //for each object in slideMenus Array, ref 1st ul element within that object and set the value of the ul elements left style property to 0px
}
document.getElementById("head").onclick = closeSlide;
document.getElementById("main").onclick = closeSlide;
}
function showSlide() {
var slideList = this.id + ".ul[0]"; //stores object ref to the 1st ul element nested within the current object.
if (currentSlide !== null) {
closeSlide();
} else {
closeSlide();
currentSlide = slideList;
currentSlide.style.display = "block";
timeID = setInterval("moveSlide()", 1);
}
}
function closeSlide() {
if (currentSlide !== null) {
clearInterval(timeID);
currentSlide.style.left = "0px";
currentSlide.style.display = "none";
currentSlide = null;
}
}
function moveSlide() {
if ((leftPos + 5) <= 220) {
currentSlide.style.left = leftPos + "px"
} else {
clearInterval(timeID);
leftPos = 0;
}
}
答案 0 :(得分:0)
只是看看你的代码,我注意到你有这一行
var slideList = this.id + ".ul[0]"; //stores object ref to the 1st ul element nested
看起来你正试图抓住ul数组的第一个元素,但是因为你把它放在引号中,你的浏览器可能会把它当作字符串来代替。假设this.id等于字符串“ul-element”,此变量将输出“ul-element.ul [0]”作为字符串,而不是将元素设置为ul [0]。
出于好奇,您是否考虑过使用jquery让您的网站执行此操作?它可以节省相当多的时间和头痛,但如果你为个人项目做这件事,我理解这样做:)
-Frank
答案 1 :(得分:0)
var slideList = this.getElementsByTagName(“ul”)[0];