jQuery UI 4 按钮。代码重复了4次,我们可以缩短它吗

发布于 2024-11-09 06:41:13 字数 598 浏览 0 评论 0原文

$('#elmLeft').button({
            icons: {
                primary: "ui-icon-triangle-1-w"
            },
            text: false
        });
$('#elmRight').button({
            icons: {
                primary: "ui-icon-triangle-1-e"
            },
            text: false
        });
$('#elmTop').button({
            icons: {
                primary: "ui-icon-triangle-1-n"
            },
            text: false
        });
$('#elmBottom').button({
            icons: {
                primary: "ui-icon-triangle-1-s"
            },
            text: false
        });
$('#elmLeft').button({
            icons: {
                primary: "ui-icon-triangle-1-w"
            },
            text: false
        });
$('#elmRight').button({
            icons: {
                primary: "ui-icon-triangle-1-e"
            },
            text: false
        });
$('#elmTop').button({
            icons: {
                primary: "ui-icon-triangle-1-n"
            },
            text: false
        });
$('#elmBottom').button({
            icons: {
                primary: "ui-icon-triangle-1-s"
            },
            text: false
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁年轻赶紧闹 2024-11-16 06:41:13
function setButton($element, icon) {
    $element.button({
            icons: {
                primary: icon
            },
            text: false
        });
}
setButton($('#elmLeft'), "ui-icon-triangle-1-w");
setButton($('#elmRight'), "ui-icon-triangle-1-e");
setButton($('#elmTop'), "ui-icon-triangle-1-n");
setButton($('#elmBottom'), "ui-icon-triangle-1-s");

或者,您可以只采用选择器而不是 jQuery 元素,并且只采用字符串的最后一个字母,但这似乎有点矫枉过正。

function setButton($element, icon) {
    $element.button({
            icons: {
                primary: icon
            },
            text: false
        });
}
setButton($('#elmLeft'), "ui-icon-triangle-1-w");
setButton($('#elmRight'), "ui-icon-triangle-1-e");
setButton($('#elmTop'), "ui-icon-triangle-1-n");
setButton($('#elmBottom'), "ui-icon-triangle-1-s");

Alternatively you could just take the selector instead of a jQuery element, and only the last letter of the string, but that seems like overkill.

一萌ing 2024-11-16 06:41:13

您可以将图标差异放入一个对象中,并使用它来更改primary

var icons = {
    elmLeft:   'w',
    elmRight:  'e',
    elmTop:    'n',
    elmBottom: 's'
};
$('#elmLeft, #elmRight, #elmTop, #elmBottom').each(function() {
    $(this).button({
        icons: { primary: 'ui-icon-triangle-1-' + icons[this.id] },
        text:  false
    });
});

我不知道您是否会认为 DRY 足以保证额外的复杂性。如果您在按钮上有一个类,那么您可以使用以下方法使其更清晰:

$('.some_class').each(function() {
    // As above.
});

或者您可以向 icons 添加一个函数,以将 id 和图标信息保持在一起:

var icons = {
    elmLeft:   'w',
    elmRight:  'e',
    elmTop:    'n',
    elmBottom: 's',
    button: function(id) {
        $('#' + id').button({
            icons: { primary: 'ui-icon-triangle-1-' + this[id] },
            text:  false
        });
    }
};
icons.button('elmLeft');
icons.button('elmRight');
icons.button('elmTop');
icons.button('elmBottom');

或者采取更进一步:

var buttons = {
    ids: {
        elmLeft:   'w',
        elmRight:  'e',
        elmTop:    'n',
        elmBottom: 's'
    },
    create: function() {
        for(id in this.ids) {
            if(this.ids.hasOwnProperty(id)) {
                $('#' + id').button({
                    icons: { primary: 'ui-icon-triangle-1-' + this.ids[id] },
                    text:  false
                });
            }
        }
    }
};
buttons.create();

如果您想要一个更干净的命名空间,您可以使用自执行函数来执行上述操作:

(function() {
    var ids = {
        elmLeft:   'w',
        elmRight:  'e',
        elmTop:    'n',
        elmBottom: 's'
    };
    for(id in ids) {
        if(ids.hasOwnProperty(id)) {
            $('#' + id').button({
                icons: { primary: 'ui-icon-triangle-1-' + ids[id] },
                text:  false
            });
        }
    }
})();

You could put the icon differences in an object and use that to alter primary:

var icons = {
    elmLeft:   'w',
    elmRight:  'e',
    elmTop:    'n',
    elmBottom: 's'
};
$('#elmLeft, #elmRight, #elmTop, #elmBottom').each(function() {
    $(this).button({
        icons: { primary: 'ui-icon-triangle-1-' + icons[this.id] },
        text:  false
    });
});

I don't know if you'd consider that DRY enough to warrant the extra complexity though. If you had a class on the buttons then you could make it cleaner with:

$('.some_class').each(function() {
    // As above.
});

Or perhaps you could add a function to icons to keep the id and icon information together:

var icons = {
    elmLeft:   'w',
    elmRight:  'e',
    elmTop:    'n',
    elmBottom: 's',
    button: function(id) {
        $('#' + id').button({
            icons: { primary: 'ui-icon-triangle-1-' + this[id] },
            text:  false
        });
    }
};
icons.button('elmLeft');
icons.button('elmRight');
icons.button('elmTop');
icons.button('elmBottom');

Or take it one step further:

var buttons = {
    ids: {
        elmLeft:   'w',
        elmRight:  'e',
        elmTop:    'n',
        elmBottom: 's'
    },
    create: function() {
        for(id in this.ids) {
            if(this.ids.hasOwnProperty(id)) {
                $('#' + id').button({
                    icons: { primary: 'ui-icon-triangle-1-' + this.ids[id] },
                    text:  false
                });
            }
        }
    }
};
buttons.create();

You could do the above with a self-executing function if you wanted a cleaner namespace:

(function() {
    var ids = {
        elmLeft:   'w',
        elmRight:  'e',
        elmTop:    'n',
        elmBottom: 's'
    };
    for(id in ids) {
        if(ids.hasOwnProperty(id)) {
            $('#' + id').button({
                icons: { primary: 'ui-icon-triangle-1-' + ids[id] },
                text:  false
            });
        }
    }
})();
请恋爱 2024-11-16 06:41:13

我确信它可以变得更短,但据我所知,不是以一种优雅的方式,而且简洁将以牺牲可理解性为代价。

I'm sure it could be made shorter, but not in an elegant way as far as i can see, and the brevity would come at a cost of understandability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文