jQuery UI 4 按钮。代码重复了4次,我们可以缩短它吗
$('#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者,您可以只采用选择器而不是 jQuery 元素,并且只采用字符串的最后一个字母,但这似乎有点矫枉过正。
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.
您可以将图标差异放入一个对象中,并使用它来更改
primary
:我不知道您是否会认为 DRY 足以保证额外的复杂性。如果您在按钮上有一个类,那么您可以使用以下方法使其更清晰:
或者您可以向
icons
添加一个函数,以将id
和图标信息保持在一起:或者采取更进一步:
如果您想要一个更干净的命名空间,您可以使用自执行函数来执行上述操作:
You could put the icon differences in an object and use that to alter
primary
: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:
Or perhaps you could add a function to
icons
to keep theid
and icon information together:Or take it one step further:
You could do the above with a self-executing function if you wanted a cleaner namespace:
我确信它可以变得更短,但据我所知,不是以一种优雅的方式,而且简洁将以牺牲可理解性为代价。
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.