简体   繁体   English

Javascript 更改鼠标 hover 上的图像不起作用

[英]Javascript change image on mouse hover not working

I have an html code as shown below.我有一个 html 代码,如下所示。 So far it is able to display the intended image.到目前为止,它能够显示预期的图像。 I also tried to change the src attribute and works completely fine.我还尝试更改 src 属性并完全正常工作。

<article id="memory-game">
    <a class="image"><img src="../../images/memory-game.png" id="memory-game" alt="" /></a>
    <h3 class="major">MEMORY GAME</h3>
    <p>Tkinter<br><br>A card-matching memory game implemented in Tkinter GUI;</p>
    <a href="#" class="special2" style="color: rgba(125, 125, 125, 1);">Confidential Source Code</a>
</article>

Now I have this script tag at the bottom of my element.现在我的元素底部有这个脚本标签。 It is supposed to change the image of the above html code from png to gif upon mouse hover on the specified article id.在指定文章id上鼠标hover上,应该将上述html代码的图像从png更改为gif。 Last time I used this code was about 3 months ago, and it doesn't seem to work now.上次我使用这个代码大约是 3 个月前,现在它似乎不起作用。

<script>
    $(function() {
        $("#memory-game").hover(
            function() {$(this).attr("src", "../../images/memory-game.gif");},
            function() {$(this).attr("src", "../../images/memory-game.png");}
        );
    });
</script>

I'm a javascript noob.我是 javascript 菜鸟。 Anybody could help me solve the png-to-gif-change-on-mouse-hover issue?任何人都可以帮我解决 png-to-gif-change-on-mouse-hover 问题吗? Would be greatly appreciated.将不胜感激。

You have 2 elements using the same id .您有 2 个元素使用相同的id The id should be unique per document.每个文档的id应该是唯一的。 jQuery is matching the first id so setting the src attribute on the div doesn't have any impact. jQuery 与第一个id匹配,因此在div上设置src属性没有任何影响。

Remove the id from the article as follow从文章中删除id如下

<article>
    <a class="image"><img src="../../images/memory-game.png" id="memory-game" alt="" /></a>
    <h3 class="major">MEMORY GAME</h3>
    <p>Tkinter<br><br>A card-matching memory game implemented in Tkinter GUI;</p>
    <a href="#" class="special2" style="color: rgba(125, 125, 125, 1);">Confidential Source Code</a>
</article>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM