代码之家  ›  专栏  ›  技术社区  ›  Fran

Jquery Addclass不工作

  •  -2
  • Fran  · 技术社区  · 14 年前

    我有这个代码,但它不起作用;我不知道为什么。我在所有浏览器(IE、chrome、firefox)中都尝试过。我想系统必须添加类,我尝试了chrome控制台来查看DOM。我看到了添加的类,但在chrome或其他浏览器中没有变化。

    <html>
      <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    
        <style>
          #boton
          {
            width:200px;
            height:30px;
            border:1px solid;
            background-color:#cccccc;
          }
    
    
        </style>
    
        <style>
          .botoncla
          {
            background-color:#FF0000;
          }
        </style>
    
    
        <script>
    
    
    
        $(document).ready(function(){
          $("#boton").hover(function(){
            $(this).addClass("botoncla");
          }, function(){
            $(this).removeClass("botoncla");
         });
        });
    
        </script>
        </head>
    
        <body>
           <div id="boton">Hello</div>
        </body>
    
    </html>
    

    有人能测试一下吗?这对我不起作用。

    3 回复  |  直到 14 年前
        1
  •  2
  •   Ifnot    14 年前

    您的jQuery代码正在工作,但CSS不工作,因为 background-color 属于 .botoncla 不过量 背景颜色 属于 #boton 。

    试试这个:

    .botoncla
    {
        background-color:#FF0000 !important;
    }
    

    编辑

    然而,在您的情况下, !important 是黑客。。。也许你应该使用 .animate() 相反:

    $(this).animate({ backgroundColor: "FF0000" }, "slow");
    
        2
  •  2
  •   MULTIDOTS    14 年前

    你可以试试这个,

    #boton.botoncla {
    background-color:#FF0000;
    }
    
        3
  •  1
  •   Nix    14 年前

    这里有几处不对。首先,您包含了两次jQuery和jQueryUI(未使用)。两个jQuery版本也不同 latest 指的是 1.7.2 建筑删除其中一个(可能是v1.4)。

    它没有应用来自 .botoncla ,是因为ID总是比类具有更高的特异性,这意味着 #boton 被认为更重要。您可以添加 !important 对宣言,或者更好的是,使其更加具体: #boton.botoncla

    查看我的jsFiddle: http://jsfiddle.net/TheNix/rvUtT/