jQuery 如果变量 = 数字,做 X,否则什么都不做

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

If variable = number, do X, else do nothing

jqueryif-statementexpandvar

提问by dylfreak6494

I wrote a jquery code that is going to expand certain elements on hover. I want the code to only expand if not expanded. Here is my code. It doesn't seem to be working correctly.

我写了一个 jquery 代码,它将在悬停时扩展某些元素。我希望代码仅在未扩展时扩展。这是我的代码。它似乎无法正常工作。

$(document).ready(function() {
    var labelstatus = 0;
});
$(document).ready(function() {
    $("a.rm").hover(function () {
        if (labelstatus != 1){
            $("#expensereduction,#envimpact,#mtextlft,#initaud,#energaud").fadeOut();
            var currentFontSize = $('.initiate').css('font-size');
            var currentFontSizeNum = parseFloat(currentFontSize, 10);
            var newFontSize = currentFontSizeNum*3;
            $('.initiate').delay(500).animate({
                fontSize: newFontSize
            });
            return false;
            var labelstatus = 1;
        }
        else {
        }
    });
});

采纳答案by Guffa

You are declaring local variables inside the functions, so they are independent of each other. Also, you get a new variable each time, so the previous value doesn't persist.

您在函数内部声明局部变量,因此它们彼此独立。此外,您每次都会获得一个新变量,因此之前的值不会持续存在。

Also, you are calling returnbefore assigning the variable, which will exit out of the function, so the assignment will never happen.

此外,您return在分配变量之前调用,这将退出函数,因此分配永远不会发生。

You need to declare the variable in a scope outside the functions:

您需要在函数之外的范围内声明变量:

$(document).ready(function() {

  var labelstatus = 0;

  $("a.rm").hover(function () {
    if (labelstatus != 1){
      $("#expensereduction,#envimpact,#mtextlft,#initaud,#energaud").fadeOut();
        var currentFontSize = $('.initiate').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize);
        var newFontSize = currentFontSizeNum * 3;
        $('.initiate').delay(500).animate({
            fontSize: newFontSize
        });
        labelstatus = 1;
        return false;
    }
    else {
    }
  });

});

Also: the parseFloatfunction doesn't have a radix parameter.

另外:该parseFloat函数没有基数参数。

回答by

this...

这个...

    return false;
    var labelstatus = 1;

should be...

应该...

    labelstatus = 1;
    return false;


...and this...

...和这个...

$(document).ready(function() {
    var labelstatus = 0;
});

should be...

应该...

var labelstatus = 0;


  • Code that comes after the returnstatement will not run

  • Using varmakes labelstatuslocal to the function

  • You really don't need two $(document).ready(function() {calls. Put your code in one, and you can make labelstatuslocal to that handler so that all code inside can use it.

  • return语句之后的代码将不会运行

  • 使用var使labelstatus函数本地化

  • 你真的不需要两个$(document).ready(function() {电话。将您的代码合二为一,您可以将其labelstatus设为该处理程序的本地代码,以便其中的所有代码都可以使用它。

回答by TimWolla

labelstatuswon't be set to 1as the function is already returned in the line before.

labelstatus不会被设置1为该函数已经在之前的行中返回。