gpt4 book ai didi

javascript - 传递两个不同参数的更短方法

转载 作者:行者123 更新时间:2023-11-28 13:29:15 24 4
gpt4 key购买 nike

对不好的标题表示歉意,不确定如何描述我正在做的事情,我的代码是这样的:

        if (is_title === 'false') {
$elem.popover({
placement: 'auto',
trigger: 'hover',
html: true,
delay: {"show": 0, "hide": 100},
container: $elem,
animation: true,
template: '<div class="popover main_block popover_tweaks" style="width: 500px;" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>'
});
} else {
$elem.popover({
placement: 'auto',
trigger: 'hover',
html: true,
delay: {"show": 0, "hide": 100},
container: $elem,
animation: true,
template: '<div class="popover main_block popover_tweaks" style="width: 500px;" role="tooltip"><div class="arrow"></div><div class="block_header"><h2 class="popover-title"></h2></div><div class="popover-content"></div></div>'
});

}

我是一个(想要的)php 开发人员,所以我尝试了这个(认为它会像数组一样工作,我会添加最后一个参数,但没有执行)

        $elem.popover({
placement: 'auto',
trigger: 'hover',
html: true,
delay: {"show": 0, "hide": 100},
container: $elem,
animation: true,
});
if (is_title === 'false') {
$elem.popover({

template: '<div class="popover main_block popover_tweaks" style="width: 500px;" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>'
});
} else {
$elem.popover({

template: '<div class="popover main_block popover_tweaks" style="width: 500px;" role="tooltip"><div class="arrow"></div><div class="block_header"><h2 class="popover-title"></h2></div><div class="popover-content"></div></div>'
});

}

我的问题是,有没有办法只更改模板参数,而不重复 if else 中的所有其他参数?

最佳答案

您正在传递一个对象文字。您可以先用公共(public)部分进行初始化,然后根据逻辑添加特定部分。

例如:

var param = {
placement: 'auto',
trigger: 'hover',
html: true,
delay: {"show": 0, "hide": 100},
container: $elem,
animation: true,
}

if (is_title === 'false') {
param.template = '<div class="popover main_block popover_tweaks" style="width: 500px;" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>';
} else {
param.template = '<div class="popover main_block popover_tweaks" style="width: 500px;" role="tooltip"><div class="arrow"></div><div class="block_header"><h2 class="popover-title"></h2></div><div class="popover-content"></div></div>';
}
$elem.popover(param);

或者,您可以在对象文字中使用条件,因此您可以执行如下操作:

$elem.popover({
placement: 'auto',
trigger: 'hover',
html: true,
delay: {"show": 0, "hide": 100},
container: $elem,
animation: true,
template: is_title ?
'<div class="popover main_block popover_tweaks" style="width: 500px;" role="tooltip"><div class="arrow"></div><div class="block_header"><h2 class="popover-title"></h2></div><div class="popover-content"></div></div>'
:'<div class="popover main_block popover_tweaks" style="width: 500px;" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>'
});

关于javascript - 传递两个不同参数的更短方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26409886/

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