为什么这个下拉菜单在IE中不起作用?

时间:2009-09-22 15:10:53

标签: javascript html css internet-explorer drop-down-menu

我有一个下拉菜单,我找到了tutorial

在Firefox和Opera中,下拉菜单工作正常。但在Internet Explorer中它不起作用。子菜单未对齐。它们不是放在父项下面,而是放在右边。

为什么它在IE中不起作用? JavaScript代码中是否存在错误,应该使其在IE中运行?

我的用户说它在IE 7.0.6002.18005和IE 8.0.6中不起作用。

只有在我认为缺少doctype的情况下才会使用怪癖模式。我的文档中有doctype(没有位置2的空格):

< !DOCTYPE html PUBLIC“ - // W3C // DTD XHTML 1.0 Strict // EN”“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

HTML:

<ul id="nav">
<li class="menuentry"><a href="#">Main item 1</a>
    <ul>
        <li><a href="#">Sub item 1</a></li>
        <li><a href="#">Sub item 2</a></li>
        <li><a href="#">Sub item 3</a></li>
    </ul>
</li>
<li class="menuentry"><a href="#">Main item 2</a>
    <ul>
        <li><a href="#">Sub item 1</a></li>
        <li><a href="#">Sub item 2</a></li>
        <li><a href="#">Sub item 3</a></li>
    </ul>
</li>
</ul>

CSS:

ul#nav li ul {
    position: absolute;
    left: -9999px;
    top: 100%;
    display: block;
    width: 100px;
    background-color: transparent;
}
ul#nav li {
    position: relative;
    float: left;
}
/* LINKS IN DROP DOWN LISTS START */
ul#nav li ul li a, ul#nav li#current ul li a {
    clear: left;
    display: block;
    text-decoration: none;
    width: 100px;
    background-color: #333;
    color: #fff;
}
ul#nav li ul li a:hover, ul#nav li#current ul li a:hover {
    text-decoration: none;
    background-color: #ececec;
    color: #333;
}
/* LINKS IN DROP DOWN LISTS END */
/* CHANGE BETWEEN VISIBLE AND INVISIBLE START */
ul#nav li:hover ul, #nav li.sfhover ul {
    left: auto;
}
/* CHANGE BETWEEN VISIBLE AND INVISIBLE END */

JavaScript的:

sfHover = function() {
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
        addEvent(sfEls[i], "mouseover", function() {
            this.className+=" sfhover";
        });
        addEvent(sfEls[i], "mouseout", function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
        });
    }
}

function addEvent(el, name, func) {
    if (el.attachEvent)
        el.attachEvent("on" + name, func);
    else
        el.addEventListener(name, func, false);
}

addEvent(window, "load", sfHover);

2 个答案:

答案 0 :(得分:1)

IE7在auto页边距方面存在问题。只需将悬停边距从left: auto更改为left: 0px,它就可以正常工作。

答案 1 :(得分:0)

如何在ul和li项目上添加填充/边距设置如下:

ul#nav li ul {
        position: absolute;
        left: -9999px;
        top: 100%;
        display: block;
        width: 100px;
        background-color: transparent;
        padding-left:0px;
        margin-left:0px;
}
ul#nav li {
        position: relative;
        float: left;
        list-style-type: none;
        padding-left:0px;
        margin-left:0px;
}
/* LINKS IN DROP DOWN LISTS START */
ul#nav li ul li a, ul#nav li#current ul li a {
        clear: left;
        display: block;
        text-decoration: none;
        width: 100px;
        background-color: #333;
        color: #fff;
}
ul#nav li ul li a:hover, ul#nav li#current ul li a:hover {
        text-decoration: none;
        background-color: #ececec;
        color: #333;
}
/* LINKS IN DROP DOWN LISTS END */
/* CHANGE BETWEEN VISIBLE AND INVISIBLE START */
ul#nav li:hover ul, #nav li.sfhover ul {
        left: auto;
}
/* CHANGE BETWEEN VISIBLE AND INVISIBLE END */