【问题标题】:Javascript onmouseover not working with doctype in firefox?Javascript onmouseover 不能在 Firefox 中使用 doctype?
【发布时间】:2009-07-21 23:47:53
【问题描述】:

我正在为我的网站上的菜单使用 JavaScript onmouseover 事件,但是当我声明一个 doctype 时,它​​在 Firefox 中不起作用。如果我不声明 doctype IE 显示页面错误。这是我使用的方法。

loadImage1 = new Image();
loadImage1.src = "http://broken.gif"; 
staticImage1 = new Image();
staticImage1.src = "http://broken.gif";

loadImage2 = new Image();
loadImage2.src = "http://broken.gif";
staticImage2 = new Image();
staticImage2.src = "http://broken.gif";

loadImage3 = new Image();
loadImage3.src = "http://broken.gif";
staticImage3 = new Image();
staticImage3.src = "http://broken.gif";

function showa() {
    image1.src=loadImage1.src;
}

function hidea() {
    image1.src=staticImage1.src;
}

function showb() {
    image2.src=loadImage2.src;
}

function hideb() {
    image2.src=staticImage2.src;
}

function showc() {
    image3.src=loadImage3.src;
}

function hidec() {
    image3.src=staticImage3.src;
}

在体内:

    <a href="http://broken.html" onMouseOver="showa()" onmouseout="hidea()">
        <img name="image1" src="http://broken.gif" alt="Browse" width="193" height="47" border="0" />
    </a>

    <a href="http://broken.html" onmouseover="showb()" onmouseout="hideb()">
        <img name="image2" src="http://broken.gif" width="193" height="47" alt="Make a List" border="0" />
    </a>

    <a href="http://broken.html" onmouseover="showc()" onmouseout="hidec()">
        <img name="image3" src="http://broken.gif" width="193" height="47" alt="Requests" border="0" />
    </a>
</div>

<div id="searchbar">
    <img  src="..broken.gif" width="222" height="41" />
    <img src="..broken.gif" width="108" height="41" alt="Search" />

【问题讨论】:

标签: javascript html cross-browser doctype onmouseover


【解决方案1】:

Firefox 不喜欢您按名称引用图像的方式。请改用 Id 和 getElementById。

编辑。请注意 Ben Blank 的出色评论,由于某种原因,WMD 无法正确显示在帖子中。

【讨论】:

  • 换句话说,在您的显示/隐藏函数中,将“image#.src =”替换为“document.getElementById("image#").src =”并在正文中将“&lt;img name=”更改为“&lt;img id=
【解决方案2】:

好的,我得到了它与 javascript 的合作。我不得不说我对这个社区印象深刻,感谢您的所有帮助。现在我要学会用正确的方式做这件事;)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多