[英]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.