简体   繁体   English

Hover 效果与 jquery 未更改为默认背景颜色

[英]Hover effect with jquery not changing to default background color

Problem: I want to add hover effect in the tr element but I have a background color assigned directly that is coming from the database.问题:我想在tr元素中添加 hover 效果,但我直接分配了来自数据库的背景颜色。 The hover effect works with jquery when I do a mouseover the color changes, but when I remove the mouse the background color doesn't go back to the previous.当我将鼠标悬停在颜色变化上时,hover 效果与 jquery 一起使用,但是当我移开鼠标时,背景颜色不会 go 回到以前。

I know this is not the best way to achieve this, if you have any ideia, please share it.我知道这不是实现这一目标的最佳方法,如果您有任何想法,请分享。

I already try adding a class to the tr element and with css define a hover effect to change the background color, but it didn't work.我已经尝试将 class 添加到tr元素并使用 css 定义 hover 效果来更改背景颜色,但它不起作用。

<table id="waypointsTable" class="table table-hover">
                <thead class="thead-dark">
                    <tr>
                        <th scope="col">Nº Pedido</th>
                        <th scope="col">Estado</th>
                        <th scope="col">Comentário</th>
                        <th scope="col">Promotor</th>
                        <th scope="col">Produtor CDM</th>
                        <th scope="col">Sala</th>
                        <th scope="col">Data Evento</th>
                        <th scope="col">Título</th>
                        <th scope="col">Data Criação</th>
                    </tr>
                </thead>
                <tbody>
                        <?php 
                        $requests = $app_R->getrequest($request_id, $user_id);

                        for($i=0; $i<count($requests); $i++){
                          $status = $app_R->getstatus($requests[$i]['Status_id']);
                          $status_color=$status[0]['color'];
                        echo'<tr class="clickable-row" style="background:'.$status_color.';" data-href="index.php?id='.$requests[$i]['Request_id'].'">';
                        echo'<th scope="row">'.$requests[$i]['Request_id'].'</th>';

                            echo '<td>'.$status[0]['name'].'</td>';
                            echo '<td>'.$status[0]['comment'].'</td>';
                            echo '<td>'.$requests[$i]['Promoter'].'</td>';
                            echo '<td>'.$requests[$i]['CDM_produtor'].'</td>';
                            echo '<td>'.$requests[$i]['Room'].'</td>';
                            echo '<td>'.$requests[$i]['Event_date'].'</td>';
                            echo '<td>'.$requests[$i]['Event_Title'].'</td>';
                            echo '<td>'.$requests[$i]['Creation_mod_date'].'</td>';
                            echo'</tr>';
                }
                ?>
                </tbody>
            </table>

Jquery to hover effect Jquery 转 hover 效果

$('#waypointsTable tr').hover(function() {
     $(this).attr('style','background: yellow');
 }, function() {
     $(this).attr("style","background: '.$status_color.'");
 });

As your variable is a php variable, you cannot use it in your javascript like that - instead I would add it to a data attribute on the table row and then change the colour that way由于您的变量是 php 变量,因此您不能像这样在 javascript 中使用它 - 相反,我会将其添加到表格行的数据属性中,然后以这种方式更改颜色

Update php to include data attribute更新 php 以包含数据属性

echo'<tr class="clickable-row" style="background:'.$status_color.';" data-original-color="'.$status_color.'" data-href="index.php?id='.$requests[$i]['Request_id'].'">';

Update js to use data attribute instead of php variable更新 js 以使用数据属性而不是 php 变量

$('#waypointsTable tr').hover(function() {
   $(this).css('background', 'yellow');
}, function() {
   var original = $(this).data('original-color');
   $(this).css('background', original);
});

I have also changed the js to use .css instead of replacing the entire style attribute (in case something else needs to add styles to the tr)我还更改了 js 以使用.css而不是替换整个样式属性(以防其他需要添加 styles 到 tr)

Please try this请试试这个

In javascript在 javascript

 $('#waypointsTable tr').hover(function() {
     $(this).css('background' ,'yellow');
 }, function() {
     $(this).css("background',<?php echo $status_color; ?>);
 });

In CSS在 CSS

<style>
 #waypointsTable tr:hover {
  background-color: yellow;
}
</style>

Try changing ' to ":尝试将 ' 更改为 ":

$(this).attr("style","background: " + $status_color + ";");

EDIT: I don't expect it was php.. duhh..编辑:我不希望它是 php .. duhh ..

Remove ' then:删除 ' 然后:

$(this).attr("style","background: $status_color;");

Try this.尝试这个。 This should works.这应该有效。 Also Get status_color in your jquery.还可以在您的 jquery 中获取status_color

$(function () {
        $("#waypointsTable tr").mouseover(function () {
            $(this).attr("style","background: " + status_color);
        });
        $("#waypointsTable tr").mouseout(function () {
           $(this).css('background' ,'yellow');
        });
    });

TLDR; TLDR; Why use JavaScript?为什么使用 JavaScript?

It's a bit hard to solve this problem without posting any current CSS styling.如果不发布任何当前的 CSS 样式,解决这个问题有点困难。 If you add the 'css tag' to your question, please to provide the CSS.如果您在问题中添加“css 标签”,请提供 CSS。

The <tr> element is the parent of <td> element. <tr>元素是<td>元素的父元素。 You can use the hover effect on the <tr> (see code-example) to change the background of the <td> .您可以在<tr>上使用 hover 效果(参见代码示例)来更改<td>的背景。 Because it's a child it will fill the space accordingly.因为它是一个孩子,它会相应地填充空间。

 tr:hover td { background: lightblue; }
 <table id="waypointsTable" class="table table-hover"> <thead class="thead-dark"> <tr> <th scope="col">Nº Pedido</th> <th scope="col">Estado</th> <th scope="col">Comentário</th> <th scope="col">Promotor</th> <th scope="col">Produtor CDM</th> <th scope="col">Sala</th> <th scope="col">Data Evento</th> <th scope="col">Título</th> <th scope="col">Data Criação</th> </tr> </thead> <tbody> <tr class="clickable-row" style="background: blue"> <td>Name</td> <td>comment</td> <td>Promoter</td> <td>CDM productor</td> <td>Room</td> <td>Event date</td> <td>Event title</td> <td>Creation date</td> </tr> <tr class="clickable-row" style="background: green"> <td>Name</td> <td>comment</td> <td>Promoter</td> <td>CDM productor</td> <td>Room</td> <td>Event date</td> <td>Event title</td> <td>Creation date</td> </tr> <tr class="clickable-row" style="background: yellow"> <td>Name</td> <td>comment</td> <td>Promoter</td> <td>CDM productor</td> <td>Room</td> <td>Event date </td> <td>Event title</td> <td>Creation date</td> </tr> <tr class="clickable-row" style="background: pink"> <td>Name</td> <td>comment</td> <td>Promoter</td> <td>CDM productor</td> <td>Room</td> <td>Event date</td> <td>Event title</td> <td>Creation date</td> </tr> <tr class="clickable-row" style="background: pink"> <td>Name</td> <td>comment</td> <td>Promoter</td> <td>CDM productor</td> <td>Room </td> <td>Event date</td> <td>Event title</td> <td>Creation date</td> </tr> <tr class="clickable-row" style="background: pink"> <td>Name</td> <td>comment</td> <td>Promoter</td> <td>CDM productor</td> <td>Room</td> <td>Event date</td> <td>Event title</td> <td>Creation date</td> </tr> </tbody> </table>

Try this尝试这个

 $('#waypointsTable tr').mouseOver(function() {
     $(this).style.color = "yellow";
 });

 $('#waypointsTable tr').mouseOut(function() {
     $(this).style.color = <?php echo $status_color; ?>;
 });

W3School Reference W3School 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM