最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing background color with addClass in jQuery - Stack Overflow

programmeradmin5浏览0评论

I am learning jQuery, and I've written a simple script that is intended to add (or remove) a class when a mouse enters (or leaves) a div. The new class adds a different height and is also supposed to change the background color and opacity of the entered div.

Here's the code:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Testing jQuery</title>
    <style type="text/css">
        div {
            width: 400px;
            height: 300px;
            margin: 20px;
            float: left;
        }


        #one {
            background-color: red;
        }

        #two {
            background-color: green;
        }

        #three {
            background-color: blue;
        }

        .change {
            height: 150px;
            background-color: rgba(0,0,0,0.4);
        }

    </style>
    <script src=".1.3/jquery.min.js"></script>
</head>
<body>
    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>

    <script type="text/javascript">

        $(document).ready(function () {
            $("div").mouseenter(function () {
                $(this).addClass("change");
            });

            $("div").mouseleave(function () {
                $(this).removeClass("change");
            });
        });

    </script>

</body>
</html>

While the height changes, there's no effect on the background-color property. Why is that so?

I've checked out the .mouseenter(), .mouseleave() and the .addClass() API documentation, but couldn't find anything specific to this problem.

I am learning jQuery, and I've written a simple script that is intended to add (or remove) a class when a mouse enters (or leaves) a div. The new class adds a different height and is also supposed to change the background color and opacity of the entered div.

Here's the code:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Testing jQuery</title>
    <style type="text/css">
        div {
            width: 400px;
            height: 300px;
            margin: 20px;
            float: left;
        }


        #one {
            background-color: red;
        }

        #two {
            background-color: green;
        }

        #three {
            background-color: blue;
        }

        .change {
            height: 150px;
            background-color: rgba(0,0,0,0.4);
        }

    </style>
    <script src="https://ajax.googleapis./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>

    <script type="text/javascript">

        $(document).ready(function () {
            $("div").mouseenter(function () {
                $(this).addClass("change");
            });

            $("div").mouseleave(function () {
                $(this).removeClass("change");
            });
        });

    </script>

</body>
</html>

While the height changes, there's no effect on the background-color property. Why is that so?

I've checked out the .mouseenter(), .mouseleave() and the .addClass() API documentation, but couldn't find anything specific to this problem.

Share Improve this question asked Sep 1, 2015 at 20:47 Dust_In_The_WindDust_In_The_Wind 3,6929 gold badges50 silver badges87 bronze badges 1
  • 2 You do realise you can do this in CSS alone, right? – Bram Vanroy Commented Sep 1, 2015 at 21:07
Add a ment  | 

5 Answers 5

Reset to default 1

Your javaScript code is 100% fine. The problem is in CSS You have written. Selection by id #one is more important than classname .one, so when you apply some #rules and .rules to the emenet at the same time, browser will chose the first ones as more important. If You can, try not to use #id selectors at all in css and leave it for javaScript usage.

css:

.one {
    background-color: red;
}

.two {
    background-color: green;
}

.three {
    background-color: blue;
}

html:

<div class="one"></div>
<div class="two"></div>
<div class="three"></div>

demo: https://jsfiddle/cz8v1gy4/

Edited answer after a re-read:

You're running in to CSS specificity issues. IDs are more specific than classes, which means that your background color is going to stay the color of the ID and not the class. This is one reason why it's (often) remended to not use IDs in CSS.

If you change everything over to classes it will work how you're expecting:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Testing jQuery</title>
    <style type="text/css">
        div {
            width: 400px;
            height: 300px;
            margin: 20px;
            float: left;
        }


        .one {
            background-color: red;
        }

        .two {
            background-color: green;
        }

        .three {
            background-color: blue;
        }

        .change {
            height: 150px;
            background-color: rgba(0,0,0,0.4);
        }

    </style>
    <script src="https://ajax.googleapis./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <div class="one"></div>
    <div class="two"></div>
    <div class="three"></div>

    <script type="text/javascript">

        $(document).ready(function () {
            $("div").mouseenter(function () {
                $(this).addClass("change");
            });

            $("div").mouseleave(function () {
                $(this).removeClass("change");
            });
        });

    </script>

</body>
</html>

Try utilizing css selectors #one:hover, #two:hover, #three:hover , :hover pseudo-class, transition . See also Specificity

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Testing jQuery</title>
    <style type="text/css">
        div {
            width: 400px;
            height: 300px;
            margin: 20px;
            float: left;
        }


        #one {
            background-color: red;
        }

        #two {
            background-color: green;
        }

        #three {
            background-color: blue;
        }

        #one:hover, #two:hover, #three:hover {
            height: 150px;
            background-color:rgba(0,0,0,0.4);
            transition: height 1000ms, background-color 1000ms;
        }

    </style>
</head>
<body>
    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>
</body>
</html>

There are some good answers here and explanations why it's not working, but it looks like no one has answered how to do what it seems like your intent was.

Which was to add the two background colors together to get a darker color on hover. You can get around it changing it to a grey by adding something else in.

For example:

.change {
    height: 150px;
    background-image: linear-gradient(to right, rgba(0,0,0,0.4) 0%,rgba(0,0,0,0.4) 100%)!important;
}

This puts a solid color gradient in as the background image which layers on top of the background color.

Like This JSFiddle

When you hover over div#one it bees div#one.changed. Now with regard to background color you have two peting values:

#one {
  background-color: red;
}

And:

.change {
  background-color: rgba(0,0,0,0.4);
}

The first one wins, because it is more specific.

To get around this use

.change {
  height: 150px;
  background-color: rgba(0,0,0,0.4)!important;
}
发布评论

评论列表(0)

  1. 暂无评论