:hover 不是一个 DOM 元素,而是一个状态。 jquery的css()函数使用inline-css。 inline-css 不能应用于不存在的元素。
如果你想改变 :hover 类的效果,我会使用 .addClass() 添加一个类,例如 alternative-over 并使用 css 设置样式。
使用纯 jQuery 的方法也可以这样说:
$('a').on("hover", function(){
$(this).css({your styles});
});
第三种选择是通过 jQuery 在 DOM 中的 <style> 标记之间添加 css,但我不推荐它,因为它会变得混乱。
编辑:我将按照您的要求尝试使用您的特定代码创建一个示例,但我现在盲目地这样做,不能保证它会立即工作。可能需要稍作调整
//Specify the parents
var obj = $('.openerp .nav-pills > li.active, .openerp .nav-pills > li.active, .openerp a.list-group-item.active, .openerp a.list-group-item.active');
//This makes it grab the "a" within the objects(parents) you've specified before
$('a', obj).on("hover", function(){
//This is what happens when you enter the area with your mouse
$(this).css({'color': res["left_hover_font_color"],'background-color': res["left_hover_bg_color"]});
},
function(){
//this is what happens when your mouse leaves the area (removes the :hover)
$(this).attr("style", ""); //resets the inline-styling
});
^ 以上是纯jQuery方式。我自己会使用 css 来获得你想要的东西。添加和删除类,像这样;
//Specify the parents
var obj = $('.openerp .nav-pills > li.active, .openerp .nav-pills > li.active, .openerp a.list-group-item.active, .openerp a.list-group-item.active');
//This makes it grab the "a" within the objects(parents) you've specified before
$('a', obj).on("hover", function(){
//This is what happens when you enter the area with your mouse
$(this).addClass("alternative-hover");
},
function(){
//this is what happens when your mouse leaves the area (removes the :hover)
$(this).removeClass("alternative-hover");
});