简体   繁体   English

悬停文本菜单时更改IMG src

[英]Change IMG src when hover a text menu

Currently I am using the code below to change the image but what I needed to do is change the image the same time I hover at the title link of the image 目前,我正在使用下面的代码来更改图像,但是我需要做的就是在将鼠标悬停在图像的标题链接的同时更改图像

onmouseover="this.src='Images/img.png'" onmouseout="this.src='Images/img2.png'"

For example when someone hover to the title link eg 'Chicken Menu' the image below it will change from img.png to img2.png wherein img.png is a front page and img2.png is where my list of menu can be found 例如,当有人将鼠标悬停在标题链接(例如“鸡肉菜单”)上时,下面的图像将从img.png更改为img2.png,其中img.png是首页,而img2.png是可以找到我的菜单列表的位置

If you want to do this using only CSS you can use the adjacent sibling combinator (+). 如果要仅使用CSS进行此操作,则可以使用相邻的同级组合器 (+)。

considering ur HTML as - 考虑到您的HTML为-

<div id="headingtext">HEADING</div>
<div id="imgDiv"></div>

CSS can be - CSS可以是-

#headingtext:hover + #imgDiv {
    background-image: url("https://images.blogthings.com/thesingleflowertest/flower-3.png");
    background-repeat:no-repeat;
    height:500px;
}

#imgDiv {
    background-image: url("https://images.blogthings.com/thesingleflowertest/flower-5.png");
    background-repeat:no-repeat;
    height:500px;
}

Fiddle Demo : http://jsfiddle.net/3z8CN/ 小提琴演示: http : //jsfiddle.net/3z8CN/

You can do something like this : 您可以执行以下操作:

<a href="#">Link</a>
<img src="http://www.blogwmp.com/wp-content/uploads/2009/08/google-logo-100x100.png" alt="">

<script>
    $('a').hover(function(){
        $('img').attr("src", "http://www.betaarchive.com/forum/download/file.php?avatar=16617_1318156786.png");             
    }, function(){
        $('img').attr("src", "http://www.blogwmp.com/wp-content/uploads/2009/08/google-logo-100x100.png");             
    });
</script>

Here's the Fiddle 这是小提琴

So with .hover() you change the attribute src of the image and you replace it by your other image. 因此,使用.hover()可以更改图像的属性src ,并将其替换为其他图像。 .hover() has parameter onMouseOver and onMouseOut so when the mouse leaves the link we put back the initial image. .hover()具有onMouseOveronMouseOut参数,因此当鼠标离开链接时,我们放回初始图像。

check the fiddle... 检查小提琴...

fiddle 小提琴

you need to add the jquery 您需要添加jQuery

--HTML-- --HTML--

<ul>
    <li>
        <span class='my_span'>this is image</span>
        <img src="http://dummyimage.com/100x100/000000/fff"/>
    </li>
    <li>
        <span class='my_span'>this is image</span>
        <img src="http://dummyimage.com/100x100/000000/fff"/>
    </li>
</ul>

-- jquery -- -jQuery-

$('span.my_span').hover(
    function(){
        $(this).next('img').attr("src","http://dummyimage.com/100x100/888888/000");
    },
    function(){
        $(this).next('img').attr("src", "http://dummyimage.com/100x100/000000/fff");             
    }
);

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

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