javascript - 以编程方式向插件函数添加选项

标签 javascript jquery

假设我有以下代码:

 framework7.actions([
            // First buttons group
            [
                // Group Label
                {
                    text: 'Here comes some optional description or warning for actions below',
                    label: true
                },
                // First button
                {
                    text: 'Alert',
                    onClick: function () {
                        framework7.alert('He Hoou!');
                    }
                },
                // Another red button
                {
                    text: 'Nice Red Button ',
                    red: true,
                    onClick: function () {
                        framework7.alert('You have clicked red button!');
                    }
                },
            ],
            // Second group
            [
                {
                    text: 'Cancel',
                    bold: true
                }
            ]
        ]);

在上面的例子中,如果某个条件成立,我如何只添加“第一个按钮”的选项/代码。例如(if(need_first_button){})。如果条件为假,那么我们不会将该按钮传递给插件。

“另一个红色按钮”也是如此。我如何才能仅在 ( if (need_red_button) {} ) 为 true 时才包含该选项?

希望这是有道理的。

谢谢。

最佳答案

先创建参数,然后根据需要修改它,最后将其传递给框架函数:

var params = [
            // First buttons group
            [
                // Group Label
                {
                    text: 'Here comes some optional description or warning for actions below',
                    label: true
                }
            ],
            // Second group
            [
                {
                    text: 'Cancel',
                    bold: true
                }
            ]
        ];

if(needFirstButton){
    params[0].push({
        text: 'Alert',
        onClick: function () {
            framework7.alert('He Hoou!');
        }        
    });
}

if(needRedButton){
        params[0].push({
        text: 'Nice Red Button ',
        red: true,
        onClick: function () {
            framework7.alert('You have clicked red button!');
        }       
    }); 
}

framework7.actions(params); 

您可以轻松添加新组,只需将数组推送到 params 而不是 params[x]

params.push([
        {
            text: 'New group button 1',
            red: true,
            onClick: function () {
                framework7.alert('You have clicked red button!');
            }       
        }
]);

上面添加了一个包含一个按钮的新组,但您可以通过用逗号分隔对象来向其中添加更多按钮。

关于javascript - 以编程方式向插件函数添加选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23712157/

相关文章:

javascript - 如果 anchor 内部有 iframe,则无法单击 anchor

javascript - 时间格式的正则表达式,如 DD :HH:MM:SS

javascript - 如何为每个 URL 显示不同的页面操作图标

jquery - CSS 可见性隐藏或折叠不起作用

java - Jquery到Java方法调用问题

javascript - Chart JS Y轴标注

javascript - 导航上没有背景颜色

javascript - 如何使用装饰器向类添加函数

javascript 无法理解 Windows 8 Promise

javascript - jQuery .load 按元素 ID