2011-03-22 20 views
1

我想完成,當鼠標懸停一個div,他們剛剛懸停展示div的對應div。比如,當您將鼠標懸停在帖子上時,其他選項會自動顯示出來?呃,我做到了。我的問題是,鼠標懸停的GET ID只是最新的一個。jQuery鼠標懸停得到不正確的ID

比方說,我有這樣的:

<div id="answer-22" class="answer-sep"> 

Answer goes here. Lorem impulse dolor... 

<div class="answer-footer"> 
<a title="permalink" class="timestamp-answer" href="#">1 hours, 29 minutes ago</a> by an anonymous poster 
<div class="answer-options" id="options-22" style="float:right;display:none;"> 

<a id="22" href="#" class="close">[edit]</a> | <a id="22" href="#" class="close">[delete]</a> | <a id="22" href="#" class="report">[report answer]</a> 
</div> <!-- end answer-options --> 
</div> <!-- end answer footer --> 
</div> <!-- end answer-sep --> 


<div id="answer-21" class="answer-sep"> 

Answer goes here. Lorem impulse dolor... 

<div class="answer-footer"> 
<a title="permalink" class="timestamp-answer" href="#">2 hours, 11 minutes ago</a> by an anonymous poster 
<div class="answer-options" id="options-21" style="float:right;display:none;"> 

<a id="21" href="#" class="close">[edit]</a> | <a id="21" href="#" class="close">[delete]</a> | <a id="21" href="#" class="report">[report answer]</a> 
</div> <!-- end answer-options --> 
</div> <!-- end answer footer --> 
</div> <!-- end answer-sep --> 

<div id="answer-20" class="answer-sep"> 
Answer goes here. Lorem impulse dolor... 
<div class="answer-footer"> 

<a title="permalink" class="timestamp-answer" href="#">4 hours, 49 minutes ago</a> by an anonymous poster 
<div class="answer-options" id="options-20" style="float:right;display:none;"> 

<a id="20" href="#" class="close">[edit]</a> | <a id="20" href="#" class="close">[delete]</a> | <a id="20" href="#" class="report">[report answer]</a> 

</div> <!-- end answer-options --> 
</div> <!-- end answer footer --> 
</div> <!-- end answer-sep --> 

正如你可以看到,它的資料覈實的名單。現在,讓我們說我有這樣的:

<script type="text/javascript"> 
$(function() { 
    $(".answer-sep").mouseenter(function(){ 
     var getID = $('.answer-sep').attr("id"); 
     var theID = getID.split("-"); 
     alert('Trying to show ID: #'+theID[1]+' and -- ' + getID); 
     $('#options-'+theID[1]).slideUp("slow"); 
     }); 
    }); 
</script> 

每當我翻轉任何的div ..我得到Trying to show ID: #22 and -- 22這是不對的。它只是最高的一個。幫幫我?

+0

你能否把一個[JS小提琴演示(http://jsfiddle.net/)讓我們一起玩,到看看發生了什麼? – 2011-03-22 23:08:08

回答

3

嘗試$(this)試試這個:

$(function() { 
     $(".answer-sep").mouseenter(function(){ 
      var getID = $(this).attr("id"); 
      var theID = getID.split("-"); 
      alert('Trying to show ID: #'+theID[1]+' and -- ' + getID); 
      $('#options-'+theID[1]).slideUp("slow"); 
      }); 
}); 
+0

非常感謝! – nn2 2011-03-22 23:22:52

4

這是您用來初始化var getID的選擇器。如果jQuery函數僅對單個元素有意義(如attr),則$('.answer-sep')將在選擇器返回的最後一個元素上執行該函數。

代替$('.answer-sep')