javascript - jQuery .hover() and page load - Stack Overflow

I have the following divs:<div id="foo">...<div id="bar" class="cont

I have the following divs:

<div id="foo">
...
    <div id="bar" class="container hidden">
    ...
    </div>
</div>

I use Twitter Bootstrap 3 and I added the hidden class to automatically hide the second div by default.

I have the following code:

$(document).ready(function() {
    $("#foo").hover(
        function() {
            $("#bar", this).toggleClass("hidden");
        }, function() {
            $("#bar", this).toggleClass("hidden");
    });
});

It seems to work correctly BUT if the mouse if already hover the #foo div during the page load, it applies by default the hover effect. After that if I move the mouse outside the #foo div, it applies again the toggleClass and displays the inner #bar div.

How to prevent this behaviour on page load?

I have the following divs:

<div id="foo">
...
    <div id="bar" class="container hidden">
    ...
    </div>
</div>

I use Twitter Bootstrap 3 and I added the hidden class to automatically hide the second div by default.

I have the following code:

$(document).ready(function() {
    $("#foo").hover(
        function() {
            $("#bar", this).toggleClass("hidden");
        }, function() {
            $("#bar", this).toggleClass("hidden");
    });
});

It seems to work correctly BUT if the mouse if already hover the #foo div during the page load, it applies by default the hover effect. After that if I move the mouse outside the #foo div, it applies again the toggleClass and displays the inner #bar div.

How to prevent this behaviour on page load?

Share Improve this question edited Apr 16, 2014 at 14:50 j08691 208k32 gold badges269 silver badges280 bronze badges asked Apr 16, 2014 at 14:42 jreanjrean 134 bronze badges 3
  • Is it within $(document).ready()? – Scimonster Commented Apr 16, 2014 at 14:46
  • yeah, could you try that? – Aldren Terante Commented Apr 16, 2014 at 14:47
  • Context selector is useless here, id should be unique. – Karl-André Gagnon Commented Apr 16, 2014 at 14:52
Add a ment  | 

3 Answers 3

Reset to default 4

This is a proper way to do it:

$("#foo").hover(
    function() {
        $("#bar", this).addClass("hidden");
    }, function() {
        $("#bar", this).removeClass("hidden");
});

No one mention it but hover pseudo event accept in/out handler, so you could toggle class:

$("#foo").hover(function () {
    $("#bar").toggleClass("hidden");
});

And as IDs must be unique on document context, no need to pass context, just target element with ID.

Instead of using toggleClass() try using addClass() and removeClass():

$("#foo").hover(

    function() {

        $("#bar", this).addClass("hidden");
    }, function() {

        $("#bar", this).removeClass("hidden");
});

This way allows you to have a more precise control over the elements.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745387493a4625503.html

相关推荐

  • javascript - jQuery .hover() and page load - Stack Overflow

    I have the following divs:<div id="foo">...<div id="bar" class="cont

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信