dot_0620 2012-09-07 03:36 采纳率: 100%
浏览 68
已采纳

jquery Hover和while循环

I have a table of php with while loop to show the records.

I added a jQuery hover handler to do that, if hovered it show a message in same row but the problem is, if hover it show a message in all rows.

Here is the css:

<style>
  .tansa{
    position: absolute;
    margin-right: -60px;
    margin-top:-25px;
    background: #CBDFF3; border: 1px solid #4081C3; font-size: 0.88em;
    padding: 2px 7px; display: inline-block;
    border-radius: 8px; -moz-border-radius: 8px; -webkit-border-radius: 8px;
    line-height: 1.2em; 
    text-shadow: 0 0 0em #FFF;
    overflow: hidden;
    text-align: center;
    color:black;
    display:none;
  }

  .arrow{ 
    position: relative;
    width: 0;
    height: 0;
    top: -25px;
    border-top: 5px solid #000000;
    border-right: 5px solid transparent;
    border-left: 5px solid transparent;
    display:none;
  }
</style>

Here is my php:

<table><tr>row</tr>
<?php
$results = mysql_query("select * from MyTable");
while{$r = mysql_fetch_array($results)){
echo "<tr><td>Row : <img src='img/tans.png' width='24' height='24' class='tansef' /><span      class='tansa' >the message</span><div class='arrow'></div></td></tr>";
}
?>
</table>

Here is jquery

$(document).ready(function(){
  $('.tansef').hover(function(){
    var sh = $('.tansa');
    var sharrow = $('.arrow');
    sh.show();
    sharrow.show();
  }, function(){
    var shs = $('.tansa');
    var sharrows = $('.arrow');
    shs.hide();
    sharrows.hide();
  });
});

Any solution to show the message in each row only?

  • 写回答

1条回答 默认 最新

  • dpyu7978 2012-09-07 03:42
    关注

    Try changing:

    var sh = $('.tansa');
    var sharrow = $('.arrow');
    

    to:

    var sh = $(this).siblings('.tansa');
    var sharrow = $(this).siblings('.arrow');
    

    (as well as the equivalent shs and sharrows variables). Your code is referencing all elements with a specific class instead of those elements relative to the one being hovered over by using this.

    jsFiddle example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效