jQuery - 如果element有类这样做 [英] jQuery - If element has class do this

查看:150
本文介绍了jQuery - 如果element有类这样做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个jQuery脚本,它将查看是否有任何元素具有特定的类并执行更改位置等操作。

I need an jQuery script that will see if any element has an specific class and do an action like change position.

这是方法,但我不喜欢认为这样可行。

This is the way, but I don`t think this will work.

$("a.contact").toggle(function() {
    $("#contact").animate({
        right: '0'
    }, 2000);

    if ($("#about").hasClass("opened")) {
        $("#about").animate({
            right: -700 + "px"
        }, 2000);
    }
}, function() {
    $("#contact").animate({
        right: -700 + "px"
    }, 2000);
});


推荐答案

首先,你在条件中遗漏了一些括号:

First, you're missing some parentheses in your conditional:

if ($("#about").hasClass("opened")) {
  $("#about").animate({right: "-700px"}, 2000);
}

但您也可以将其简化为:

But you can also simplify this to:

$('#about.opened').animate(...);

如果 #about 没有打开类,它不会动画。

If #about doesn't have the opened class, it won't animate.

如果问题在于动画本身,我们需要要了解有关元素定位的更多信息(绝对?绝对内部相对父级?父级是否有布局?)

If the problem is with the animation itself, we'd need to know more about your element positioning (absolute? absolute inside relative parent? does the parent have layout?)

这篇关于jQuery - 如果element有类这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆