【问题标题】:Adding css attribute hover using JQuery使用 JQuery 添加 css 属性悬停
【发布时间】:2016-04-12 17:15:05
【问题描述】:

当我尝试向此类添加 CSS 属性时遇到问题,它给了我一个错误

我的代码

$('.openerp .nav-pills > li.active a:hover, .openerp .nav-pills > li.active a:focus, .openerp a.list-group-item.active a:hover, .openerp a.list-group-item.active a:focus').css({'color': res["left_hover_font_color"],'background-color': res["left_hover_bg_color"]});

错误:

未捕获的错误:语法错误,无法识别的表达式:不支持的伪:悬停

为什么悬停在这里会导致问题?

【问题讨论】:

  • res["left_hover_font_color"] 是什么?
  • @j08691 一个数组。虽然我会把它变成一个对象并称之为res.left_hover_font_color,但这是一个选择。
  • 你有这方面的 jsfiddle 吗?
  • @ Babydead @j08691 是的,我使用字典
  • 您在 JQUERY 选择语句中使用 a:hover 过滤器,而 a:hover 不是有效过滤器

标签: javascript jquery css


【解决方案1】:

JQuery 选择器与 css 选择器相似但不完全相同,如果您想定位诸如悬停或焦点之类的事件,您需要使用回调,例如:

$('.openerp .nav-pills > li.active a').hover(function() {
    $(this).css({'color': res["left_hover_font_color"],'background-color': res["left_hover_bg_color"]});
},function() {
    $(this).css({'color': "",'background-color': ""});
});

【讨论】:

  • 我的代码有问题,我不知道为什么
【解决方案2】:

:hover 不是一个 DOM 元素,而是一个状态。 jquery的css()函数使用inline-cssinline-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");
});

【讨论】:

  • 您能否将您所说的应用于我的示例或代码以便更好地理解
  • 两者都不起作用:(我尝试了第一个,我认为它应该起作用,但它不起作用
  • 您可以尝试从“var obj”中删除其他选项吗?所以,现在,只取1? `var obj = $('.openerp .nav-pills > li.active');另外,您是否遇到任何(新)错误?在这种情况下,你应该告诉我,这会有很大的不同
  • 我现在看到我的代码中有错字。如果你试图盲目地复制粘贴而不阅读它,我觉得你会遇到麻烦并继续提问。你应该试着从你得到的答案中学习。我很高兴别人的答案对你有用,但如果你对答案不做任何事情,我也会预测那个问题。无论如何,祝你好运。
猜你喜欢
  • 2012-11-22
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
相关资源
最近更新 更多