我尝试对表中的某一行应用addClass 和removeClass,但由于某种原因,addClass 和removeClass 样式未应用。 由于它们不起作用,我尝试使用 css,但这使我的代码更加冗余,将相同的样式应用于某些字段。任何帮助,将不胜感激。谢谢
来自 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 {
}
我已经在 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');
});
最佳答案
使用 css()
调用,您可以创建内联样式。这些总是覆盖样式表中定义的那些(除非使用 !important
,但最好避免这种情况)。
重点是,如果您使用 css()
,那么如果类名定义了相同的属性,则它们将不适用。首先删除您添加的 CSS 样式 ({'color':'', 'font-weight':''}
),然后类名称应就位。
关于javascript - addClass/removeClass 不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10689300/