简体   繁体   English

addClass / removeClass不适用

[英]addClass/removeClass doesn't apply

I tried to apply the addClass and removeClass for one of the rows in my table and for some reason the addClass and removeClass styles are not applied. 我试图为我表中的一行应用addClass和removeClass,由于某种原因,不应用addClass和removeClass样式。 Since they were not working I tried to use css but this one is making my code more redundant applying the same style to some of the fields. 由于它们没有工作,我尝试使用css,但这个使我的代码更加冗余,将相同的样式应用于某些字段。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

From firebug 从萤火虫

<tr style="color: red; font-weight: bold; background: none repeat scroll 0% 0% rgb(255, 255, 255); cursor: pointer;" class="state-selected"><td style="padding:2.5px 0px 2.5px 2px;">Equipment</td></tr>



.state-selected {
    }

and I have defined the style on the top of my jsp page 我已经在jsp页面的顶部定义了样式

<style type="text/css">
.state-selected {
    color: 'red';
    font-weight: 'bold';
}
 </style>    
$("#index tr:gt(0)").hover(

    function () {
        $(this).css({
            'background': '#e9e7e7',
            'cursor': 'pointer'
        });
    }, function () {
        $(this).css({
            'background': '#ffffff',
            'cursor': 'pointer'
        });
    }).click(function (e) {
        $("#index tr").removeClass('state-selected');
        $(this).addClass('state-selected');

    });

Using the css() call, you are creating inline styles. 使用css()调用,您将创建内联样式。 These always override those defined in stylesheets (except when !important is used, but that's best avoided). 它们总是覆盖样式表中定义的那些(除非使用!important ,但最好避免使用)。

The point is, if you use css() then class names won't apply if they define the same properties. 关键是,如果使用css()那么如果类名定义相同的属性,则不会应用类名。 First remove the CSS styles you added ( {'color':'', 'font-weight':''} ) and then the class name should fall into place. 首先删除你添加的CSS样式( {'color':'', 'font-weight':''} ),然后类名应该落到位。

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

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