简体   繁体   English

在jQuery中悬停表行时存储背景颜色

[英]Store background-color when hover a table row in jQuery

I have an ASP.NET GridView . 我有一个ASP.NET GridView Each row has a different color depending on the value of one of the displayed fields. 每行具有不同的颜色,具体取决于其中一个显示字段的值。 There are two possible values therefore there can be two different colors. 有两个可能的值因此可以有两种不同的颜色。

Now I want to highlights the rows on the GridView hovered by the mouse. 现在我想突出显示由鼠标悬停的GridView上的行 The below script works perfecty but once I hover the mouse out the color becomes white for any row. 下面的脚本工作得很完美,但是一旦我将鼠标悬停在外,任何一行的颜色都会变为白色。

I would like to know if there is a way to somehow store the "original" color of the row when the mouse hovers in and put it back once the mouse hovers out. 我想知道是否有一种方法可以在鼠标悬停时以某种方式存储行的“原始”颜色,并在鼠标悬停时将其放回原处。

          $(document).ready(function() {
            $("#<%=gdUpdateProduct.ClientID%> tr:has(td)").hover(function() {
                    $(this).css("background-color", "Lightgrey");
                }, function() {
                    $(this).css("background-color", "#ffffff");
                });

            });

I tried this solution that seems quite logical to me but it does not work because the script does not store the value of color once it finishes to execute: 我尝试过这个对我来说似乎很合理的解决方案,但它不起作用,因为脚本一旦完成执行就不会存储颜色的值:

$(document).ready(function() {
            $("#<%=gdUpdateProduct.ClientID%> tr:has(td)").hover(function() {
                    var color = $(this).css("background-color");
                    $(this).css("background-color", "Lightgrey");
                }, function() {
                    $(this).css("background-color", "#ffffff");
                });
            });

Anybody might provide a solution? 有人可能会提供解决方案吗? Thanks 谢谢

You could store the previous (original) value in the data property: 您可以将以前(原始)值存储在data属性中:

$(document).ready(function() {
    $("#<%=gdUpdateProduct.ClientID%> tr:has(td)").hover(function() {
        var $this = $(this);

        if (!$this.data('originalBg')) { // First time, no original value is set.
            $this.data('originalBg', $this.css('background-color')); // Store original value.
        }
        $this.css("background-color", "Lightgrey");
    }, function() {
        var $this = $(this);

        $this.css("background-color", $this.data('originalBg'));
    });
});

If you're using HTML5 , you can set the data property directly in the <tr> element ( docs ): 如果您使用的是HTML5 ,则可以直接在<tr>元素( docs )中设置data属性:

<tr style="background-color: #abc123;" data-originalBg="#abc123"> ... </tr>

That way, you can simply get away with: 这样,你可以简单地逃脱:

$(document).ready(function() {
    $("#<%=gdUpdateProduct.ClientID%> tr:has(td)").hover(function() {
        $(this).css("background-color", "Lightgrey");
    }, function() {
        $(this).css("background-color", $this.data('originalBg')); // Already set via the data-originalBg attribute of the `tr`
    });
});

If your highlight colour is static (which it appears to be) then an alternative approach would be to create a class called something like : 如果您的突出显示颜色是静态的(看起来是这样),那么另一种方法是创建一个类似于以下内容的类:

.highlight {
    background-color: #efefef !important;
} 

and then simply: 然后简单地说:

$("#<%=gdUpdateProduct.ClientID%> tr:has(td)").hover(function() {
    $(this).addClass("highlight");
}, function() {
    $(this).removeClass("highlight");
});

And you'll get the original background colour back for free (tested in chrome 24.0.1312.57 m and FF 18.0.2 on win 7). 并且您将获得免费的原始背景颜色(在第7场比赛中以铬24.0.1312.57米和FF 18.0.2进行测试)。

Toby 托比

Have you tried 你有没有尝试过

var color = '';
$(document).ready(function() {
    $("#<%=gdUpdateProduct.ClientID%> tr:has(td)").hover(
        function() {
            color = $(this).css("background-color");
            $(this).css("background-color", "Lightgrey");
        }, 
        function() {
            $(this).css("background-color", color);
        });
    });
});

This way the varaible is global so will remember the value. 这种变量是全局的,所以会记住价值。

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

相关问题 表格行的background-color属性(jQuery) - background-color attribute of table row (jQuery) jQuery背景颜色已更改,悬停背景颜色不起作用 - jQuery background color changed, hover background-color not working 有条件地改变表格行的背景颜色 - conditionally change the background-color of table row 使用jQuery更改背景时,链接会失去其CSS背景颜色悬停属性 - Links lose their css background-color hover attribute when changing background with jQuery 使用 jquery 动态更改背景颜色后,用于背景颜色的 CSS 悬停选择器不起作用 - CSS hover selector for background-color not working after dynamic change of background-color with jquery 悬停菜单(Jquery-Shopify)时如何更改.body的背景颜色? - How to Change background-color of .body when hover menu ( Jquery - Shopify )? jQuery:当 hover 链接时,在 div 中动画(淡化)背景颜色或图像? - jQuery: Animate (fade) background-color or image in div when hover a link? 使用 jquery 更改 hover 上的背景颜色无法正常工作 - Changing background-color on hover not working properly using jquery 使用jQuery将背景颜色和边框添加到悬停时的表格行 - Add background color and border to table row on hover using jquery 在翱翔的JQuery变动表细胞和行背景颜色 - JQuery Change Table Cell And Row Background Color On Hover
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM