jQuery +如果语句

发布于 2024-11-08 02:00:29 字数 311 浏览 1 评论 0原文

我正在尝试自学一些基本的 jquery,但在尝试使用 if 语句时遇到了问题。我的代码如下:

var animate = 0;

$('a').click(function () {
if (animate == 0) {
$('#shadow').fadeOut(500);
var animate = 1;
}
});

我希望进一步使用一些 else 语句,以便根据“animate”的值,单击时它将执行不同的 jquery 操作。我确信我忽略了一些明显的事情,但我正在用头撞墙试图弄清楚它是什么。

任何帮助将不胜感激!

I'm trying to teach myself some basic jquery and am having trouble with an if statement I'm trying to use. My code is as follows:

var animate = 0;

$('a').click(function () {
if (animate == 0) {
$('#shadow').fadeOut(500);
var animate = 1;
}
});

I'm hoping to use some else statements further down the line, so that depending on the value of "animate" it will perform a different jquery action when clicked. I'm sure I've overlooked something obvious, but I'm banging my head against a wall trying to figure out what it is.

Any help would be most appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万水千山粽是情ミ 2024-11-15 02:00:29

当您使用 var 声明变量时,它就成为局部变量,这意味着它在该范围内是新的。

实现您想要的目标的快速(但肮脏)方法如下:

var animate = 0;
$('a').click(function() {
    if (animate === 0) {
        $('#shadow').fadeOut(500);
        animate = 1;  // note the lack of "var"
    }
});

请注意,这可能是一个相当不完美的解决方案;特别是 animate 不会将自身设置回 0(不过,您可以使用回调函数作为 fadeOut 的第二个参数来执行此操作)。

更好的解决方案可能是在您正在使用的特定项目上放置(和删除)一个类:

$('a').click(function() {
    if (!$('#shadow').hasClass('animating')) {
        $('#shadow').addClass('animating').fadeOut(500, function() {
            $(this).removeClass('animating');
        });
    }
});

但是,我不知道您的实现的细节,所以我会让您弄清楚什么是适合您的特殊需求。

When you use var to declare a variable, then it becomes a local variable, which means it's new within that scope.

The quick (and dirty) way for you to get the goal you want is something like:

var animate = 0;
$('a').click(function() {
    if (animate === 0) {
        $('#shadow').fadeOut(500);
        animate = 1;  // note the lack of "var"
    }
});

Note that this is probably a pretty imperfect solution; in particular animate doesn't set itself back to 0 (you can do this with a callback function as the second argument to fadeOut, though).

A still better solution is probably to place (and remove) a class on the particular item you're working with:

$('a').click(function() {
    if (!$('#shadow').hasClass('animating')) {
        $('#shadow').addClass('animating').fadeOut(500, function() {
            $(this).removeClass('animating');
        });
    }
});

However, I don't know the details of your implementation, so I'll let you figure out what is right for your particular needs.

妞丶爷亲个 2024-11-15 02:00:29

将 1 分配给 animate 时,不应再次使用 var 关键字。通过这样做,您将导致语法错误,因为 animate 已在同一范围内声明。

You shouldn't use the var keyword again when assigning 1 to animate. By doing this, you are causing a syntax error since animate has already been declared within the same scope.

南汐寒笙箫 2024-11-15 02:00:29

您正在通过在 click 函数中使用 var 重新定义 animate。

更改

var animate = 1;

animate = 1;

This 将使您在外部范围中设置 animate 变量的值,而不是在单击函数内的范围内创建动画。

华泰

You're redefining animate by using var inside the click function.

change

var animate = 1;

to

animate = 1;

This will make it so you set the value of the animate variable in the outer scope and not animate that you are creating in scope within the click function.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文