【问题标题】:if statement within function breaks javascript函数中的 if 语句破坏了 javascript
【发布时间】:2014-10-22 03:44:46
【问题描述】:

我被这个难住了,非常感谢有人的帮助。

我正在自定义 highslide 以与 wordpress 集成。通过 highslide.config.js 文件中的以下代码,我将类名添加到某些元素,并根据某些条件通过 onClick 调用传递不同的属性。

在我添加以下代码之前一切正常:

if(hsGroupByWpGallery){
    slideshowGroup: this.parentNode.parentNode.parentNode.id
};

当上面的代码出现时,不仅那条语句没有执行,而且整个事情都停止了工作。即使 if 语句类似于 if(1=1){};还是坏了。

如果我只是使用 slideshowGroup: this.parentNode.parentNode.parentNode.id 或什么都没有(我正在寻找的两个选项),那么两者都符合我的预期。我只需要一个 if 语句来在它们之间切换。

以下是相关代码:

jQuery(document).ready(function() {
  
  var hsCustomGalleryGroupClass = 'fbbHighslide_GalleryGroup';
  var hsCustomGalleryGroupChecker = 0;
  var hsGroupByWpGallery = true;


    jQuery('.' + hsCustomGalleryGroupClass).each(function(){
        hsCustomGalleryGroupChecker++;
        return false;
    });
    if (hsCustomGalleryGroupChecker > 0){
        jQuery('.' + hsCustomGalleryGroupClass).each(function(i, $item) {
            var grpID = $item.id;
            jQuery('#' + grpID + ' .gallery-item a').addClass('highslide').each(function() {
                this.onclick = function() {
                    return hs.expand(this, {
                        slideshowGroup: grpID
                    });
                };
            });
        });
    } else {
        jQuery('.gallery-item a').addClass('highslide').each(function() {
            this.onclick = function() {
                return hs.expand(this, {
                    // This is the problem if statement
                    if(hsGroupByWpGallery){
                      slideshowGroup: this.parentNode.parentNode.parentNode.id
                    };
                });
            };
        });
    };
  
});

提前致谢。

【问题讨论】:

  • 您添加的代码无法解析...这是无效的语法。 : 应该是 = 或其他东西。我不确定这里的目标是什么。
  • 你的意思是slideshowGroup = this.parentNode.parentNode.parentNode.id
  • 什么是幻灯片组?您不能使用 ':' 分配变量,这种类型的分配在对象文字中使用,例如 {key: valye}
  • 哦....所以基于其他代码...您没有复制您的 { brackets } 或它传递给...或返回语句的函数。

标签: javascript jquery wordpress highslide


【解决方案1】:

问题是您正在尝试分配条件属性.. 在这样的对象定义中不能有 if 条件

jQuery('.gallery-item a').addClass('highslide').each(function () {
    this.onclick = function () {
        var obj = {};
        //assign the property only if the condition is tru
        if (hsGroupByWpGallery) {
            obj.slideshowGroup = this.parentNode.parentNode.parentNode.id;
        }
        return hs.expand(this, obj);
    };
});

另一种方法是

jQuery('.gallery-item a').addClass('highslide').each(function () {
    this.onclick = function () {
        //if the flag is true sent an object with the property else an empty object
        return hs.expand(this, hsGroupByWpGallery ? {
            slideshowGroup: this.parentNode.parentNode.parentNode.id
        } : {});
    };
});

【讨论】:

  • 嗯,这两个都有效。谢谢!我不完全确定第一个是如何工作的。通过声明 var obj = {};看来您正在创建一个对象,然后在 if 语句的稍后部分,您似乎正在添加一个属性 (slideshowGroup) 及其值。假设这是对的,让我感到困惑的部分是你将整个对象作为 hs.expand 的参数传递,而我认为它正在寻找的只是 obj.slideshow 的值。如何从 obj 对象中提取正确的数据?
  • 另外(因为我们在这里限制了字符):您的第二个选项是 if 语句的简写,对吗?我原以为这会遇到与标准 if 语句相同的问题。你能解释一下为什么这行得通,而常规的 if 语句却不行吗?
【解决方案2】:

根据其他代码,我认为您可能想要这个:

   jQuery('.gallery-item a').addClass('highslide').each(function() {
      this.onclick = function() {

         if(hsGroupByWpGallery){
            return hs.expand(this, {
               slideshowGroup: this.parentNode.parentNode.parentNode.id
            });
          }
       };
   });

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多