简体   繁体   English

悬停在父上时更改img src

[英]Change img src when on hover of parent

I have the following html 我有以下HTML

<div class="custom text-center threeBox ofsted">
  <a class="ofsted" title="ofsted report" href="http://reports.ofsted.gov.uk/">
    <img class="text-center ofstedLogo" src="images/ofsted_good_transparent.png" alt="ofsted good rating">
    <h3>Ofsted</h3>
    </a>
</div>

I have written the following jquery which swaps the background colours on hover of a: 我写了下面的jquery,它在a悬停时交换背景颜色:

    $(".threeBox a").hover(
        function(){ // Mouse Over
            $(this).parent().addClass("swapBg");
        },
        function(){ // Mouse Out
            $(this).parent().removeClass("swapBg");
        }
    );

Which works great, but I need to swap the img.ofstedLogo src on hover to 'OFSTED_good_logo.jpg'. 效果很好,但我需要将鼠标悬停时将img.ofstedLogo src交换为'OFSTED_good_logo.jpg'。 I've tried several changes to the jQuery code but can't get it to work. 我已经尝试过对jQuery代码进行一些更改,但无法使其正常工作。 Any ideas please? 有什么想法吗?

You can use find() to get the img and attr() for changing the image source 您可以使用find()获取imgattr()来更改图像源

 $(".threeBox a").hover( function(){ // Mouse Over $(this).parent().addClass("swapBg").find('img').attr('src','OFSTED_good_logo.jpg'); }, function(){ // Mouse Out $(this).parent().removeClass("swapBg").find('img').attr('src','images/ofsted_good_transparent.png'); } ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="custom text-center threeBox ofsted"> <a class="ofsted" title="ofsted report" href="http://reports.ofsted.gov.uk/"> <img class="text-center ofstedLogo" src="images/ofsted_good_transparent.png" alt="ofsted good rating"> <h3>Ofsted</h3> </a> </div> 

Use attr() 使用attr()

$(".threeBox a").hover(
        function(){ // Mouse Over
            $(this).parent().addClass("swapBg");
            $(this).find('img').attr('src', 'images/OFSTED_good_logo.jpg');
        },
        function(){ // Mouse Out
            $(this).parent().removeClass("swapBg");
            $(this).find('img').attr('src', 'images/ofsted_good_transparent.png');
        }
    );

This would do the job: 这将完成工作:

$(this).children('.ofstedLogo').attr('src', 'yourimagehere.png');

See attr attr

Use .find() to select the image and .attr() to change the src attribute: 使用.find()选择图像,然后使用.attr()更改src属性:

$(".threeBox a").hover(
    function(){ // Mouse Over
        $(this).parent().addClass("swapBg");
        $(this).find('img.ofstedLogo').attr("src", "images/OFSTED_good_logo.jpg");
    },
    function(){ // Mouse Out
        $(this).parent().removeClass("swapBg");
        $(this).find('img.ofstedLogo').attr("src","images/ofsted_good_transparent.png");
    }
);

There are a few ways to accomplish this effect with a graphic .. check out the jsfiddle example here: 有几种方法可以通过图形来实现此效果。在此处查看jsfiddle示例:

jQuery direct image replacement jQuery直接图像替换

$(".twoBox > a").hover(
    function(){ // Mouse Over
        $(this).find('img:first').attr("src", 'http://blog.modernica.net/wp-content/uploads/2011/12/2-300x300.png');
    },
    function(){ // Mouse Out
        $(this).find('img:first').attr("src", 'http://www.adamcentric.com/wp-content/uploads/2014/09/1-300x300.png');
    }
);

OR jQuery CSS class swap 或jQuery CSS类交换

.ofstedLogo2 {
    height: 300px;
    width:300px;
    background-image:url(http://www.adamcentric.com/wp-content/uploads/2014/09/1-300x300.png); 
}
.ofstedLogo3 {
    height: 300px;
    width:300px;
    background-image:url(http://blog.modernica.net/wp-content/uploads/2011/12/2-300x300.png); 
}
$(".threeBox > a").hover(
    function(){ // Mouse Over
        $(this).find('div:first').toggleClass("ofstedLogo2");
        $(this).find('div:first').toggleClass("ofstedLogo3");
    },
    function(){ // Mouse Out
        $(this).find('div:first').toggleClass("ofstedLogo2");
        $(this).find('div:first').toggleClass("ofstedLogo3");
    }
);

https://jsfiddle.net/rwdw5u76/ https://jsfiddle.net/rwdw5u76/

one is using an actual IMG src replace, and the other us using a css background image method instead. 一种是使用实际的IMG src替换,另一种是使用css背景图像方法。

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

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