javascript - 将鼠标悬停在表格中的特定行上时显示按钮

标签 javascript php jquery css html

当我将鼠标悬停在某一行上时,我试图显示一个按钮。 $index 变量是表中的总行数。 "#r"+a.toString() 变量指的是表行的id,"#b"+a.toString() 指的是当我悬停在某个地方时我想显示的行的按钮表的行。目前,每当我将鼠标悬停在任何行上时,它都会在最后一行显示按钮。

<script type="text/javascript">
    $(document).ready(function (){
        for(a=1; a <= <?php echo $index; ?>; a++){
        var button = $("#b"+a.toString());
        $("#r"+a.toString()+" td").mouseover(function(){
            button.css({"visibility": "visible"});
        });
        $("tr td").mouseout(function(){
            button.css({"visibility": "hidden"});
        });
    }
    });
</script>



<table class="table table-striped table-bordered table-condensed table-hover">
                    <thead>
                        <tr> 
                            <th>Item</th>
                            <th>Quantity</th>
                            <th>Price</th>
                        </tr>
                    </thead>
                    <?php 
                        $item_array;
                        $index = 0;
                        $index_2 = 1;
                        $r = "r";
                        $b="b";
                        foreach ($item_array as $id_array){ ?>

                            <tr id="<?php echo $r.$index_2; ?>">
                             <td><?php echo $item_array[$index] ?></td>
                                <td> <?php echo $quantity_array[$index] ?></td>
               <td>   
                        <form method='POST' action='edit.php'>
                            <?php echo $price_array[$index];?>
                            <button id="<?php echo $b.$index_2; ?>" type="button" style="float:right; visibility:hidden; align-content:right;" class="btn btn-sm btn-warning">Edit</button>
                        </form>
                    </td><?php
                        $index++;
                        $index_2++;

                        echo "</tr>";
                        } ?>

                </table>

最佳答案

当简单的遍历就足够时,使用 ID 会使这比需要的更复杂。

$(function(){
    $('tr').hover(function(){ /* hover first argument is mouseenter*/
        $(this).find('button').css({"visibility": "visible"});
    },function(){  /* hover second argument is mouseleave*/
       $(this).find('button').css({"visibility": "hidden"});
    });

});

为了更细化,给按钮一个通用类。

更简单的是单独使用 CSS

button.buttonCLass{ visibility: hidden;}
tr:hover button.buttonCLass{ visibility: visible;}

关于javascript - 将鼠标悬停在表格中的特定行上时显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26079997/

相关文章:

javascript - jQuery:元素列表上的交错动画

javascript - 如何更改此导航栏中 Logo 的位置?

javascript - 如何从 JavaScript 调用 php/wordpress 命令?

php - 找到长 TTFB 的罪魁祸首

javascript - 下拉菜单未调用 onChange 函数

jquery 向下滚动时动画回到原始位置(?)

javascript - 牢不可破的Javascript

javascript - 无法输入 react 输入文本字段 - onChange 无法正常工作

php - 为什么我收到第二个请求和第一个请求?没有任何值(value)

PHP:die() 必须死吗?