dstm2014 2016-01-15 06:42
浏览 814
已采纳

使用JQuery动态隐藏/显示<td>

Using PHP, I am returning a table with rows containing 3 columns each. The third column has an information icon that is hidden unless hovered over. Here's a snippet from the PHP code that generates this table:

$contoutput = $contoutput . '<tr><td>' . $xleft[$i] . '</td><td>' . $xright[$i] . '</td><td class="hiddensourceicon" onMouseOver="showicon(this);">' . findsource($xsource[$i]) . '</td></tr>';

The CSS for hiddensourceicon is simple:

.hiddensourceicon { display: none; }

This way, the said icon doesn't display upon load. I am using JQuery to remove this class, thus "unhiding" the icon upon hover:

function showicon(row2show){ $(row2show).removeClass("hiddensourceicon"); }

But for some reason, the JQuery function refuses to trigger. What am I doing wrong?

  • 写回答

4条回答 默认 最新

  • dtwxmn8741 2016-01-15 07:15
    关注

    It think the trouble comes in when you have something set as display none and are trying to hover on something that doesn't exist one way you could solve this is by using opacity instead of display. If you want to keep using display you will have to take into account something has to be there on the screen to hover on. Here is a quick solution using opacity.

    JSFiddle: https://jsfiddle.net/kriscoulson/kg6380z8/1/

    You should also stay away from using inline javascript. Such as mouseover="function(args);"

    var $hiddenicon = $('.hiddenicon');
    
    $hiddenicon.on('mouseover mouseout', function() {
      var opacity = $(this).css('opacity') == 1 ? 0 : 1;
      $(this).css({
        opacity: opacity
      });
    });
    table,
    th,
    td {
      border: 1px solid black;
    }
    .hiddenicon {
      opacity: 0;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <tr>
        <td class="hiddenicon">Icon</td>
        <td>Things in here</td>
        <td>Other things in here</td>
      </tr>
      <tr>
        <td class="hiddenicon">Icon</td>
        <td>Things in here</td>
        <td>Other things in here</td>
      </tr>
      <tr>
        <td class="hiddenicon">Icon</td>
        <td>Things in here</td>
        <td>Other things in here</td>
      </tr>
    </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站