2012-09-21 54 views
1

我想打开图片时,我将鼠标悬停在单词“PIC”上,但我对同一部分有10个或更多的这个&,他们每个人都必须显示特定图片的元素图片。悬停在文字上打开图片

<h6 id="pic1" class="pic"> PIC</h6> 
<div id="img_pic1" class="imgpic" style="display:none"><imgsrc="img/image1.jpg"/></div> 

<h6 id="pic2" class="pic"> PIC</h6> 
<div id="img_pic2" class="imgpic" style="display:none"><img src="img/image2.jpg"/>/div> 

<h6 id="pic3" class="pic"> PIC</h6> 
<div id="img_pic3" class="imgpic" style="display:none"><img src="img/image3.jpg"/>/div> 

ECT .....

<script> 
$('h6.pic').mouseenter(function(){ 
$('div.img_pic').fadeIn(); 
}).mouseleave(function(){ 
$('div.img_pic').fadeOut(); 
}); 
</script> 

Thhis做工精细,但它打开所有的图像,而不是打开我的徘徊PIC的唯一形象? 任何帮助将不胜感激。非常感谢你。

回答

1

试试这个

<script> 
    $('h6.pic').mouseenter(function(){ 
    $(this).next().fadeIn(); 
     }).mouseleave(function(){ 
    $(this).next().fadeOut(); 
    }); 
</script> 

入住这FIDDLE

而且你不关闭你DIV正确

<img src="img/image2.jpg"/>/div> 

更新的代码

UPDATED FIDDLE

如果是这样的话,那么你可以试试这个

$('h6.pic').mouseenter(function() { 
     console.log($(this).next('div')) 
     $(this).next().next('.imgpic').fadeIn(); 
    }).mouseleave(function() { 
     $(this).next().next('.imgpic').fadeOut(); 
    });​ 

// OR

$('h6.pic').mouseenter(function() { 
      console.log($(this).next('div')) 
      $(this).next().next().fadeIn(); 
     }).mouseleave(function() { 
      $(this).next().next().fadeOut(); 
     });​ 
+0

非常感谢你的工作完美。 – user1626749

+0

我还有一个小问题,当我在PIC和图像之间添加一条线时,它不再工作了,你知道一个解决方法吗?如果我将价格线放在图像的下方,价格会在图像下方,而不是留在型号#1前面。型号#:1

PIC
$ 1.00包装
对不起和谢谢 – user1626749

+0

如果这是你需要找到.imgpic类股利的情况。检查上述 –

1

你应该使用$(本) - 指的是当前元素 - 和next() - 元素右在当前元素之后

$('h6.pic').mouseenter(function(){ 
    $(this).next('div.imgpic').fadeIn(); 
}).mouseleave(function(){ 
    $(this).next('div.imgpic').fadeOut(); 
}); 

您还有一个错字lector

$('div.img_pic') // <-- your class is imgpic without the _ 
+0

非常好,工作得很好。非常感谢你。 – user1626749

+0

型号:1/H6>

PIC
user1626749

0

恕我直言,使用$(this).next()的方法有点不灵活;我想接近它略有不同的附加属性data-*指向每个标题元素的目标PIC:

<h6 id="pic1" class="pic" data-target="img_pic1"> PIC</h6> 
<div id="img_pic1" class="imgpic" style="display:none"><img src="img/image1.jpg"/></div> 

<h6 id="pic2" class="pic" data-target="img_pic2"> PIC</h6> 
<div id="img_pic2" class="imgpic" style="display:none"><img src="img/image2.jpg"/></div> 

<h6 id="pic3" class="pic" data-target="img_pic3"> PIC</h6> 
<div id="img_pic3" class="imgpic" style="display:none"><img src="img/image3.jpg"/></div>​ 

然后你jQuery'd是这样的:

$('h6.pic').each(function() { 
    var target = $(this).data('target'); 
    $(this).mouseenter(function() { 
    $('#' + target).fadeIn(); 
    }).mouseleave(function() { 
    $('#' + target).fadeOut(); 
    });   
});​ 

什么嘿,这是fiddle

+0

我试过,但没有工作,和我的编辑说:在任一版本的小提琴或更高版本上的最后一个分号之后的'声明预期'。这令人沮丧,因为这同样更有趣。你能给我一些指针吗?谢谢 – user1626749

+0

更新:我需要在PIC行和图像行之间添加一行。 型号:1

PIC
$ 1.00包装
很抱歉打扰&谢谢为你的答案。 – user1626749

+0

谢谢我发现问题,我的尾部空间是在最后一个分号之后。你的代码非常好。 – user1626749