【问题标题】:How can I have a function method in an if condition with JavaScript code?如何使用 JavaScript 代码在 if 条件下使用函数方法?
【发布时间】:2017-05-04 04:41:38
【问题描述】:

我想在我的 JavaScript 代码中给出条件方法。此代码在 Internet Explorer 10 版本中不起作用。当我将此代码粘贴到JavaScript validator 时,会显示此消息:

函数声明不应放在块中。使用函数表达式或将语句移到外部函数的顶部。

我怎样才能在这里有一个函数?

            if(jQuery("header").attr("id") == "header-style-two")
        {
                    function sticky_relocate() {
                        var window_top = jQuery(window).scrollTop();
                        var div_top = jQuery('#sticky-anchor').offset().top;
                        if (window_top > div_top) {
                            jQuery('.mainmenu-area').removeClass('light-menu');
                            //This is for when div in top         
                        } else {
                            jQuery('.mainmenu-area').addClass('light-menu');
                            //This is for when div in not top
                        }
                    }

                    jQuery(function () {
                        jQuery(window).scroll(sticky_relocate);
                        sticky_relocate();
                    }); 
        }   

【问题讨论】:

标签: javascript html function


【解决方案1】:

You can't have named function in an if block(它在规范中是无效的,无论如何提升都会使它相当混乱,但是一些 JavaScript 引擎可能会尝试补偿错误代码,这就是它可能在某些浏览器中工作的原因),但你可以分配函数到 if 块中的变量。

而不是:

function sticky_relocate() {

你可以这样做:

var sticky_relocate = function() {

所以这样的事情就可以解决问题:

if(jQuery("header").attr("id") == "header-style-two")
{
    var sticky_relocate = function() {
        var window_top = jQuery(window).scrollTop();
        var div_top = jQuery('#sticky-anchor').offset().top;
        if (window_top > div_top) {
            jQuery('.mainmenu-area').removeClass('light-menu');
            //This is for when div in top         
        } else {
            jQuery('.mainmenu-area').addClass('light-menu');
            //This is for when div in not top
        }
    }

    jQuery(function () {
        jQuery(window).scroll(sticky_relocate);
        sticky_relocate();
    }); 
}

【讨论】:

  • 首先,为什么有人需要将函数放入 if 块中?
  • 为什么我们不能放置命名函数
  • @A.T.您可能希望有条件地将函数设置为变量,或者避免创建您不想使用的函数。
  • 非常感谢#Alexander。这段代码有效,我的问题得到了解决:)
  • 在您的回答中值得一提的是,为什么我们不能在 if 块中命名函数
【解决方案2】:

在 if 条件下不能有函数声明。你可以把它放在外面,根据条件调用它。

var isHeaderStyleTwo = (jQuery("header").attr("id") == "header-style-two");
function sticky_relocate() {
  var window_top = jQuery(window).scrollTop();
  var div_top = jQuery('#sticky-anchor').offset().top;
  if (window_top > div_top) {
    jQuery('.mainmenu-area').removeClass('light-menu');
    //This is for when div in top         
  } else {
    jQuery('.mainmenu-area').addClass('light-menu');
    //This is for when div in not top
  }
}

jQuery(function () {
  if (isHeaderStyleTwo) {
      jQuery(window).scroll(sticky_relocate);
        sticky_relocate();
  }

【讨论】:

    【解决方案3】:

    JSHint 给你一个警告,意思是,“我们认为这不是最好的主意,但我们会允许你这样做。”

    您可以通过多种方式更改代码。

    将函数声明改为函数表达式:

    if(jQuery("header").attr("id") == "header-style-two") {
      var sticky_relocate = function() {
        var window_top = jQuery(window).scrollTop();
        var div_top = jQuery('#sticky-anchor').offset().top;
        if (window_top > div_top) {
          jQuery('.mainmenu-area').removeClass('light-menu');
          //This is for when div in top         
        } else {
          jQuery('.mainmenu-area').addClass('light-menu');
          //This is for when div in not top
        }
      }
      jQuery(function () {
        jQuery(window).scroll(sticky_relocate);
        sticky_relocate();
      }); 
    }
    

    或将函数定义移出if 块。

    if(jQuery("header").attr("id") == "header-style-two") {
      jQuery(function () {
        jQuery(window).scroll(sticky_relocate);
        sticky_relocate();
      }); 
    }
    
    function sticky_relocate() {
      var window_top = jQuery(window).scrollTop();
      var div_top = jQuery('#sticky-anchor').offset().top;
      if (window_top > div_top) {
        jQuery('.mainmenu-area').removeClass('light-menu');
        //This is for when div in top         
      } else {
        jQuery('.mainmenu-area').addClass('light-menu');
        //This is for when div in not top
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 1970-01-01
      • 2021-09-29
      • 2020-05-20
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多