Javascript 在父级悬停时更改img src
我有下面的htmlJavascript 在父级悬停时更改img src,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有下面的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 goo
<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>
这很好,但我需要将悬停时的img.ofstedLogo src替换为'OFSTED_good_logo.jpg'。我尝试了对jQuery代码的一些更改,但无法使其正常工作。有什么想法吗?使用
这将完成以下工作:
$(this).children('.ofstedLogo').attr('src', 'yourimagehere.png');
请参见使用以选择图像和更改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");
}
);
您可以使用获取img和更改图像源
$.threeBox a.悬停
函数{//鼠标悬停
$this.parent.addClassswapBg.find'img'。attr'src','OFSTED_good_logo.jpg';
},
函数{//鼠标退出
$this.parent.removeClassswapBg.find'img'。attr'src','images/ofsted_good_transparent.png';
}
;
有几种方法可以通过图形实现此效果。。在这里查看JSFIDLE示例: 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');
}
);
或者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");
}
);
一个是使用实际的imgsrc替换,另一个是使用css背景图像方法
$(".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');
}
);
.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");
}
);