【问题标题】:Why doesnt my simple javascript rollover work?为什么我的简单 javascript 翻转工作不起作用?
【发布时间】:2012-04-20 02:32:08
【问题描述】:

大家好,我有一个简单的鼠标悬停效果,我尝试了几个脚本,但都不起作用,谁能告诉我为什么?`

javascript:

<script language="JavaScript" type="text/javascript">
<!--
if (document.images) {
    homebuttonup = new Image();
    homebuttonup.src = "./images/gym-light.png";
    homebuttondown = new Image();
    homebuttondown.src = "./images/buttonright.png";
}

function buttondown(buttonname) {
    if (document.images) {
        document[buttonname].src = eval(buttonname + "down.src");
    }
}

function buttonup(buttonname) {
    if (document.images) {
        document[buttonname].src = eval(buttonname + "up.src");
    }
}
// -->
</script>

和链接:

    <a href="index.html" onmouseover="buttondown('homebutton')"       onmouseout="buttonup('homebutton')">
  <img id='Img4Inner' name="homebutton" src='./images/gym-light.png' alt="" />

  </a>

【问题讨论】:

  • 安装 firebug 并报告错误(如果有)
  • 在这里工作正常:jsfiddle.net/j08691/ETHaM。您是否还有其他可能会干扰的代码?
  • 您可能已经听说过,但请谨慎使用eval非常。有些人甚至争辩说永远不应该使用它。通常有更好的方法来解决问题。
  • 我在 jsfiddle 上尝试了我的代码,你的权利它确实在那里工作.. 但它在我的网站上不起作用任何其他原因为什么它不会?这是 viifit.com 上的小自行车标志,如果您查看源代码,您会看到它在那里..
  • 我尝试安装 firebug,但我认为我没有收到任何错误

标签: javascript image onmouseover rollover


【解决方案1】:

**更新 2(最后一个大声笑)** 你目前在 a 标签上有 onmouseout 和 onmouseover,将它们移动到 image 标签,它会工作:

你是代码:

<a onmouseout="buttonup('homebutton')" onmouseover="buttondown('homebutton')"        
href="http://www.[...].com" style="height:120px;width:100px;">
<img id="Img4Inner" alt="" src="http://[..].com/images/gym-light.png" name="homebutton">
</a>

工作代码:

<a onmouseout="buttonup('homebutton')" onmouseover="buttondown('homebutton')"        
href="http://www.[...].com" style="height:120px;width:100px;">
 <img alt="" src="http://[...]/images/gym-light.png"  onmouseout="buttonup(this)" 
     onmouseover="buttondown(this)" name="homebutton" id="Img4Inner">
</a>

更新:因为您正在调用锚标记上的函数,它们需要具有类似于以下的高度和宽度(相应地放置您的高度和宽度):

<a style="height:25px;width:25px;" href="http://www.[...].com" 
onmouseover="buttondown('homebutton')" onmouseout="buttonup('homebutton')">
...
</a>

我出去了……

我刚刚使用了萤火虫,用高度和宽度编辑了 HTML,效果很好:

)

虽然我确信这会解决问题.. doctype 设置为 并且应该类似于这里的内容 (LINK)

如果您实施了以下方法,则图像将具有高度和宽度,并且由于这是要定位的图像,因此可能更有意义..

http://jsfiddle.net/ETHaM/2/

if (document.images) {
    homebuttonup = new Image();
    homebuttonup.src = "http://www.placekitten.com/100/100/";
    homebuttondown = new Image();
    homebuttondown.src = "http://dummyimage.com/100x100/000/fff";
}

function buttondown(obj) {
    if (document.images) {
        obj.src = eval(obj.name+ "down.src");
    }
}

function buttonup(obj) {
    if (document.images) {
        obj.src = eval(obj.name + "up.src");
    }
}


<a href="index.html">
<img id='Img4Inner' onmouseover="buttondown(this)" onmouseout="buttonup(this)" name="homebutton" src='http://www.placekitten.com/100/100/' alt="" />
</a>

【讨论】:

  • @mplungjan 我改变了我的答案,只是提供了一个替代方案..也许它仍然无关紧要..
  • 好多了。现在摆脱 eval
  • jsfiddle.net/ETHaM/5 我已经在这里尝试过它并且它可以工作.. 但是当我从我的目录打开它或将它上传到我的主机时它不在我的索引中.. 它在 viifit 上直播。 com 带有自行车的图像(绿色图标)有连接到它的确切代码 - 链接有效,但鼠标悬停无效?
  • @user1250526 - 这是你的锚标签..他们需要一个高度和宽度
  • 遗憾的是它仍然无法正常工作,但如果我将此代码放在空白页面中,它确实可以工作,图像在 div 内可能是为什么?在该线程的底部帖子中:phpfreaks.com/forums/index.php?topic=357975.0 是我正在使用的整个代码..
【解决方案2】:

您的代码有效

这里有一个更不显眼的版本

http://jsfiddle.net/mplungjan/927nN/

<a href="index.html" id="homeLink"><img
 id='Img4Inner' class="hoverImg"
 name="homebutton" src='http://www.placekitten.com/100/100/' alt="" /></a>
 <a href="about.html" id="aboutLink"><img
 id='Img5Inner' class="hoverImg"
 name="aboutbutton" src='http://www.placekitten.com/100/100/' alt="" /></a>
var buttons={};
if (document.images) {
    buttons["homebuttonup"] = new Image();
    buttons["homebuttonup"].src = "http://www.placekitten.com/100/100/";
    buttons["homebuttondown"] = new Image();
    buttons["homebuttondown"].src = "http://dummyimage.com/100x100/000/fff";
    buttons["aboutbuttonup"] = new Image();
    buttons["aboutbuttonup"].src = "http://www.placekitten.com/100/100/";
    buttons["aboutbuttondown"] = new Image();
    buttons["aboutbuttondown"].src = "http://dummyimage.com/100x100/000/fff";
}

window.onload=function() {
    var images = document.getElementsByTagName("img");
    for (var i=0,n=images.length;i<n;i++) {
        if (images[i].className=="hoverImg") {
            images[i].parentNode.onmouseover=function() {
              var buttonname=this.id.replace("Link","button");
              document[buttonname].src = buttons[buttonname + "down"].src;
            }
            images[i].parentNode.onmouseout=function() {
              var buttonname=this.id.replace("Link","button");
              document[buttonname].src = buttons[buttonname + "up"].src;
            }
        }
    }   
}

【讨论】:

  • 这也行不通,它在 jfiddle 上效果很好,但在我的脚本中却不行,我认为因为图像在某个 div 内,我是否需要访问 div 而不是 document.images?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
相关资源
最近更新 更多