首页 文章

如何在带有JavaScript代码的if条件中使用函数方法?

提问于
浏览
3

我想在我的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();
                    }); 
        }

3 回答

  • 6

    You can't have named function in an if block(它不符合规范和提升会使它相当混乱,但是一些JavaScript引擎可能会尝试补偿不良代码,这就是为什么它可能在某些浏览器中有效),但是你可以将函数分配给变量 . 如果阻止 .

    而不是:

    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();
        }); 
    }
    
  • 1

    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
      }
    }
    
  • 0

    在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();
      }
    

相关问题