javascript 如果元素有类则运行函数,否则运行另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14525632/
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-10-26 21:51:33  来源:igfitidea点击:

If element has class then run function, else run another

javascriptjquerycsshtml

提问by Liam

I have an element on my page that I want to make draggable.

我的页面上有一个元素要使其可拖动。

On the click event I add the class 'active' this just expands my div and the image inside it.

在单击事件中,我添加了“active”类,这只会扩展我的 div 和其中的图像。

So initially my div looks like...

所以最初我的 div 看起来像......

<div class="work-showcase">

on click it becomes...

点击它变成...

<div class="work-showcase active">

I want to say if the div has class active, perform X function, else perform Y.

我想说如果 div 有活动的类,执行 X 功能,否则执行 Y。

$(body).click(function(){

        if($('.work-showcase').hasClass('active')){

            var bleft = 4900 ;
            var bright = $(window).width() - 200;

            $('.active > ul li').draggable({
                 axis: "x", 
                 revert: false,
                 stop: function(event, ui) {
                    if(ui.position.left < -bleft)
                    {   
                    $(this).animate({"left": "0px"}, 600);
                    }
                    if(ui.position.left > bright)
                    {   
                    $(this).animate({"left": "0px"}, 600);
                    }

                }
            });


            } else {


     var sleft = 1846 ;
     var sright = $(window).width() - 200;

            $('.work-showcase > ul li').draggable({
                 axis: "x", 
                 revert: false,
                 stop: function(event, ui) {
                    if(ui.position.left < -sleft)
                    {   
                    $(this).animate({"left": "0px"}, 600);
                    }
                    if(ui.position.left > sright)
                    {   
                    $(this).animate({"left": "0px"}, 600);
                    }

                }
            });

        }

    });

My If statement doesnt seem to work...

我的 If 语句似乎不起作用...

回答by Ryan Smith

Try this instead of what you currently have (let me know if there is a problem)

试试这个而不是你目前拥有的(如果有问题,请告诉我)

$('body').click(function () {
    var drag, left, right;

    if ($('.work-showcase').hasClass('active') === true) {
        drag = '.active > ul li';
        left = -4900;
        right = $(window).width() - 200;
    } else {
        drag = '.work-showcase > ul li';
        left = -1846;
        right = $(window).width() - 200;
    }

    $(drag).draggable({
        axis: 'x',
        revert: false,
        stop: function (event, ui) {
            if (ui.position.left < left) {
                $(this).animate({'left': '0px'}, 600);
            } else if (ui.position.left > right) {
                $(this).animate({'left': '0px'}, 600);
            }
        }
    });
});

回答by adeneo

This selector :

这个选择器:

$(body).click(function(){ ... });

needs to be :

需要是 :

$('body').click(function(){ ... }

notice the quotes !

注意引号!

回答by Robert Fricke

With jQuery:

使用 jQuery:

if($(this).hasClass("active")){
    //do this
} else {
    //do that
}

Edit:it returns trueor false: jQuery hasClass Documentation

编辑:它返回truefalsejQuery hasClass 文档