2012-09-20 155 views
0

提示不工作,而環 所有圖標顯示我第一個圖標的信息僅 這裏jQuery的提示是PHP與while循環

$check = odbc_exec($conn,"select * from Item"); 
while($r = odbc_fetch_array($check)){ 
$info = $r['Info']; 
$Image = $r['image']; 
echo "<span class='hint'><img src='$Image'></span> 
<div class='readitem' style='display:none'>$info</div>"; 

} 

的jQuery:提示

<script type="text/javascript"> 
$(document).ready(function() { 
    var changeTooltipPosition = function(event) { 
     var tooltipX = event.pageX - 10; 
     var tooltipY = event.pageY + 10; 
     $('div.tooltip').css({top: tooltipY, left: tooltipX});}; 
    var showTooltip = function(event) { 
    var item = $('.readitem').html(); 

     $('<div class="tooltip"><center>'+ item + '</center></div>').appendTo('body'); 
     changeTooltipPosition(event); 
    }; 
    var hideTooltip = function() { 
     $('div.tooltip').remove(); 
    }; 
    $(".hint").bind({ 
     mousemove : changeTooltipPosition, 
     mouseenter : showTooltip, 
     mouseleave: hideTooltip 
    }); 
}); 

</script> 

任何解決方案?

謝謝你..

回答

0

這是var item = $('.readitem').html();行的問題。

當你做$('.readitem')時,它會匹配頁面上具有該類的所有元素。然後,當調用.html()時,它會返回第一個匹配元素的html(因此總是向您顯示第一個元素)。

你需要做的是看你要徘徊的.hint,以找到兄弟.readitem,然後使用那個html。

所以,這應該不是工作:

var item = $(this).siblings('.readitem').html(); 

$(this)應該是在其上徘徊.hint的情況下,所以你要找到它的兄弟元素的.readitem

+0

它已經在while循環中 – DragoN

+0

您可以在原始文章中包含整個while循環嗎? – manavo

+0

我已經添加了原始的while循環我使用 – DragoN

1

我從來沒有見過沒有條件的while循環。 你的while循環應該形成像

while (condition) { 
    //your code 
} 

其中條件是評估爲真或假的表達式。 但在你的情況下,你可能有一個圖標的URL數組。您應該使用for循環更方便在數組中循環。

for ($i = 0; $i < count($images); $i++) { 
    echo $images[$i]; // Assuming $images is an array. 
} 
+0

我已經添加了我使用的原始循環 – DragoN