jQuery.addClass 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17941143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:14:18  来源:igfitidea点击:

jQuery.addClass not working

jqueryhtmlcss

提问by user2608855

This looks valid but it's not working. I'd like the 'huh' div to become opaque when the menu is hovered over. I tried this with fadein/out and it worked but just the once which was odd.

这看起来有效,但它不起作用。当菜单悬停在上面时,我希望 'huh' div 变得不透明。我用淡入/淡出尝试了这个,它奏效了,但只有一次很奇怪。

 <script type="text/javascript">
    $( function() {
        $('#menuNav').hover( function() {
            $('#huh').addClass('.opacity');
        }, function(){
            $('#huh').removeClass('.opacity');
        });
    });

</script>

.opacity {
    opacity: 0.3;
}

回答by Cherniv

Use it without dot:

不带点使用它:

 $(function(){

        $('#menuNav').hover(function(){

            $('#huh').addClass('opacity');
        }, function(){
            $('#huh').removeClass('opacity');
        });
    });

回答by Vinay

$( function() {
    $('#menuNav').hover( function() {
        $('#huh').toggleClass('opacity');
    });
});

回答by Sergio

.hover()makes many events, better to use .mouseenter(). Note alsothat when adding class you don't have the .(dot).

.hover()制作许多事件,更好地使用.mouseenter(). 另请注意,在添加类时,您没有.(点)。

$(function(){

        $('#menuNav').mouseenter(function(){

            $('#huh').addClass('opacity');
        }, function(){
            $('#huh').removeClass('opacity');
        });
    });

回答by Rohit Azad

Used this Remove .

用过这个删除 .

    $('#huh').addClass('opacity'); // remove .

 $('#huh').removeClass('opacity'); // remove .

==============

==============

Or used to

或者曾经

toggleClass in jquery


$(function(){

        $('#menuNav').hover(function(){
            $('#huh').toggleClass('opacity');
        });
    });

More about this

更多关于这个

回答by Amit

Try this

尝试这个

 <script>
    $(function(){
    $('#menuNav').hover(function(){
        $('#huh').addClass('opacity');
    }, function(){
        $('#huh').removeClass('opacity');
    });
 });

</script>

回答by bizzehdee

You have a .in your class name in addClassand removeClass, you need to add and remove without the dot when calling these methods. i.e.

.的类名中有一个addClassand removeClass,你需要在调用这些方法时添加和删除不带点的。IE

$(function() {
    $('#menuNav').hover(function(){
        $('#huh').addClass('opacity');
    }, function(){
        $('#huh').removeClass('opacity');
    });
});

回答by Falguni Panchal

.remove and then use this javascript

.删除然后使用这个javascript

 $(function(){
    $('#menuNav').hover(function(){
        $('#huh').addClass('opacity');
    }, function(){
        $('#huh').removeClass('opacity');
    });
 });

回答by Eswara Reddy

Try this

尝试这个

$("#menuNav").mouseenter(function() {
    $('#huh').addClass('opacity');
}).mouseleave(function() {
    $('#huh').removeClass('opacity');
});