【发布时间】:2014-06-08 21:39:50
【问题描述】:
我试图将鼠标悬停在一张图片上,然后让所有图片变成我正在悬停的一张图片。我已经完成了。
当我鼠标悬停时,我希望图像恢复正常,但我无法弄清楚。
我正在尝试使用 rel 选择器,但在我现在的解决方案中,当我将鼠标移出时,所有图像都会采用第一张图像的 rel 而不是一切都恢复正常。
这是我的 HTML
<div id="container">
<header id="masthead"><h1>Swap All</h1></header>
<div id="stage">
<div id="gallery">
<img class="photo" rel="_images/image1.jpeg" src="_images/image1.jpeg" />
<img class="photo" rel="_images/image2.jpeg" src="_images/image2.jpeg" />
<img class="photo" rel="_images/image3.jpeg" src="_images/image3.jpeg" />
</div>
这是我的 JS
function grabIt(){
var thisImageSrc = this.src;
$('.photo').attr('src', thisImageSrc);
}
function letItGo() {
var theRel = $('.photo').attr('rel');
$('#gallery img').attr( 'src', theRel);
console.log(theRel);
}
$('.photo').mouseover(grabIt);
$('.photo').mouseout(letItGo);
【问题讨论】:
-
加个jsfiddle链接会更好。
-
你需要遍历每个
img。