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

标签 javascript jquery

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

        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/

相关文章:

javascript - 更新 onchange 上显示的价格

Javascript Date.UTC() 与 moment.utc()

asp.net - 有没有办法访问在 ASP.Net MVC 的母版页中的 View 上定义的对象

jquery - 如何使用 JQuery 将焦点设置在输入字段上

javascript - 如何将 Mapbox Geocoder 结果地点/国家/地区文本名称获取到 JavaScript 函数中?

javascript - select option remove() 方法删除多个选项

javascript - AngularJS,附加到 $scope 的对象不会绑定(bind)到 html 元素

javascript - 需要使用 jquery 为两组不同的复选框设置单独的单击事件。

jquery - AjaxForm 在 Safari 和 Chrome 中不工作

javascript - 仅在选择单选按钮时显示表单