2012-09-21 135 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