gpt4 book ai didi

javascript - 函数内的 if 语句会破坏 javascript

转载 作者:行者123 更新时间:2023-11-30 17:13:42 24 4
gpt4 key购买 nike

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

我正在定制 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
};
});
};
});
};

});

提前致谢。

最佳答案

问题是你试图分配一个条件属性..你不能在这样的对象定义中有一个 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
} : {});
};
});

关于javascript - 函数内的 if 语句会破坏 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26499999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com