我用jq写了个脚本想实现:鼠标悬浮在页面表格中背景色为灰色的行,变更其背景色

我写了个脚本如下:
if($(".x-table tr").css('style') == "background-color:grey") {
$(this).mouseover(function () {
$(this).css("background-color","yellow");
}).mouseout(function () {
$(this).css("background-color","grey");
});}
但是执行这个脚本后,并没有生效,请问有人知道是哪里出问题了吗?我怀疑是不是“”选中背景色为灰色的行“”这个命令有问题,但我不知道咋修改。
页面的表格是灰白相间的,所以只能分开设置鼠标悬浮变更背景色:
if($(".x-table tr").css('style') == "background-color:grey") {
$(this).mouseover(function () {
$(this).css("background-color","yellow");
}).mouseout(function () {
$(this).css("background-color","grey");
});}
else if($(".x-table tr").css('style') == "background-color:white") {
$(this).mouseover(function () {
$(this).css("background-color","yellow");
}).mouseout(function () {
$(this).css("background-color","white");
});}
但是执行第一个if的时候就不行了。

不是这样判断的

是.attr('style')吧

首先,你通过$(".x-table tr").css('style')这样得到的值是undefined,不可能进判断;然后jq设置样式有几种方式,但是你用的这个是错误的,应该是$(this).css("backgroundColor","yellow");;最后,给出部分代码(rgb(128, 128, 128)对应的就是grey):

if($('.x-table tr').css('backgroundColor') == 'rgb(128, 128, 128)'){
            $('.x-table tr').mouseover(function () {
                $('.x-table tr').css("backgroundColor","yellow");
            }).mouseout(function () {
                $('.x-table tr').css("backgroundColor","grey");
            });
        }