【问题标题】:I'm trying to change my link color in jQuery. Why is it not working我正在尝试在 jQuery 中更改我的链接颜色。为什么它不起作用
【发布时间】:2013-08-14 06:01:16
【问题描述】:

这是我目前所拥有的:

$('a').hover(function(){
        ('this').css('color','#F60')
        });
    });

我的目的是让悬停在链接上的用户颜色从白色变为橙色

【问题讨论】:

  • ('this').css('color','#F60') 是错字还是您在真实代码中也缺少前导 $
  • 在 css 中可能更容易做到:a:hover { color: #f60 }

标签: jquery html hyperlink hover


【解决方案1】:
$('a').hover(function(){
        $(this).css('color','#F60')
        });
    });

参考:http://remysharp.com/2007/04/12/jquerys-this-demystified/

【讨论】:

    【解决方案2】:
    $(a).hover(function(){
        $(this).css({color: '#f60'});
    });
    

    理论上应该为您解决问题。

    【讨论】:

      【解决方案3】:

      如果你不关心 IE ≤6,你可以使用纯 CSS ...

      .forum:hover { background-color: #380606; }
      

      使用 jQuery,通常最好为这种样式创建一个特定的类:

      .forum_hover { background-color: #380606; }
      

      然后在鼠标悬停时应用该类,并在鼠标悬停时将其删除。

      $('.forum').hover(function(){$(this).toggleClass('forum_hover');});
      

      如果不能修改类,可以将原来的背景色保存在.data()(Example):

        $('.forum').data('bgcolor', '#380606').hover(function(){
          var $this = $(this);
          var newBgc = $this.data('bgcolor');
          $this.data('bgcolor', $this.css('background-color')).css('background-color', newBgc);
        });
      

        $('.forum').hover(
          function(){
            var $this = $(this);
            $this.data('bgcolor', $this.css('background-color')).css('background-color', '#380606');
          },
          function(){
            var $this = $(this);
            $this.css('background-color', $this.data('bgcolor'));
          }
        );   
      

      【讨论】:

        【解决方案4】:

        您有一些语法错误。这应该可以。

        $('a').hover(function () {
            $(this).css('color', '#F60');
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-08
          • 1970-01-01
          • 2022-11-11
          • 1970-01-01
          相关资源
          最近更新 更多