2014-10-22 91 views
1

我非常喜欢这个,非常感谢别人的帮助。if function within function breaks javascript

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

一切正常,直到我添加以下代码:

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

当上面的代码存在,不仅是一个语句不执行,但整个事情停止工作。即使if语句像if(1 = 1){};它仍然打破。

如果我只是幻灯片组: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 
 
        }; 
 
       }); 
 
      }; 
 
     }); 
 
    }; 
 
    
 
});

在此先感谢。

+0

您添加的代码不会解析...它是无效的语法。 ':'应该是一个'='或其他东西。我不确定这里的目标是什么。 – 2014-10-22 03:47:33

+1

您的意思是'slideshowGroup = this.parentNode.parentNode.parentNode.id' – 2014-10-22 03:48:04

+0

什么是slideshowGroup?你不能用一个':'赋值变量,这种类型的赋值在对象字面量内使用,例如,{key:valye} – 2014-10-22 03:49:07

回答

2

问题是你想分配条件属性..你不能有这样的

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

一个对象定义范围内的if状态的另一种方式做同样是

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 
     } : {}); 
    }; 
}); 
+0

那么,这两个工作。谢谢! 我不完全确定第一个人是如何工作的。通过声明var obj = {};看起来你正在创建一个对象,然后在if语句中,你似乎在添加一个属性(slideshowGroup)和一个值。假设这是正确的,让我感到困惑的部分是,然后你将整个对象作为hs.expand的参数传递,当我认为它正在寻找的只是obj.slideshow的值。如何从obj对象中提取正确的数据? – fbb 2014-10-22 06:54:18

+0

另外(因为我们在这里限制字符): 你的第二个选择是速记if语句,对不对?我原以为这会和标准的if语句一样受到同样的问题。你能解释一下,为什么这个工作,而经常的if语句没有? – fbb 2014-10-22 06:55:07

0

我想你可能想这样的基础上,其他代码:

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

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