jQuery:我应该如何构建由 jQuery 生成的新列表
编辑:这个问题是关于 jQuery 重构的。基本上我有一大段代码,但我想看看其他人是否能想出更好的方法来重构它。由于我是 jQuery 新手,我不确定我的设计是否走在正确的轨道上
原始帖子: 我正在开发一个书签,它将 HTML 元素添加到页面中。我正在构建一个侧边栏,它是一个 div ,里面有一个 ul 。我试图将我的样式与代码分开,并编写一些内容,以便在我将来需要进行更改时可以轻松管理它们。是否可以重构此代码以使其更干净/更高效?
myObj = {
$sidebar: {},
createSidebar: function () {
var self = this, $undo = {}, $redo = {}, $email = {}, $reset = {}
self.$sidebar = $("<div id='myObj-sidebar'><ul></ul></div>");
$undo = $('<a></a>', {
id: 'sidebar-undo',
className: 'sidebar-item',
href: '#',
text: 'Undo',
click: function (e) {
//self.doUndo();
e.preventDefault();
}
});
$redo = $('<a></a>', {
id: 'sidebar-redo',
className: 'sidebar-item',
href: '#',
text: 'Redo',
click: function (e) {
//self.doRedo();
e.preventDefault();
}
});
$email = $('<a></a>', {
id: 'sidebar-change-email',
className: 'sidebar-item',
href: '#',
text: 'Change E-Mail',
click: function (e) {
//self.createEmailDialog();
e.preventDefault();
}
});
$reset = $('<a></a>', {
id: 'sidebar-reset-all',
className: 'sidebar-item',
href: '#',
text: 'Reset All',
click: function (e) {
//self.doReset();
e.preventDefault();
}
});
self.$sidebar.find('ul').append($undo, $redo, $email, $reset);
self.$sidebar.find('.sidebar-item').wrap('<li/>');
self.$sidebar.appendTo("body");
}
}
edit: This question is about jQuery refactoring. Basically I have a big block of code but I want to see if other folks can think of a better way to refactor it. Since I'm new to jQuery I'm not sure if I'm on the right track or not with my design
original post:
I'm working on a bookmarklet which adds HTML elements to the page. I'm building a sidebar which is a div with a ul inside of it. I'm trying to keep my styles separate from my code and to also write things so that they will be easy to manage if I need to make changes in the future. Is it possible to refactor this code to make it cleaner/more efficient?
myObj = {
$sidebar: {},
createSidebar: function () {
var self = this, $undo = {}, $redo = {}, $email = {}, $reset = {}
self.$sidebar = $("<div id='myObj-sidebar'><ul></ul></div>");
$undo = $('<a></a>', {
id: 'sidebar-undo',
className: 'sidebar-item',
href: '#',
text: 'Undo',
click: function (e) {
//self.doUndo();
e.preventDefault();
}
});
$redo = $('<a></a>', {
id: 'sidebar-redo',
className: 'sidebar-item',
href: '#',
text: 'Redo',
click: function (e) {
//self.doRedo();
e.preventDefault();
}
});
$email = $('<a></a>', {
id: 'sidebar-change-email',
className: 'sidebar-item',
href: '#',
text: 'Change E-Mail',
click: function (e) {
//self.createEmailDialog();
e.preventDefault();
}
});
$reset = $('<a></a>', {
id: 'sidebar-reset-all',
className: 'sidebar-item',
href: '#',
text: 'Reset All',
click: function (e) {
//self.doReset();
e.preventDefault();
}
});
self.$sidebar.find('ul').append($undo, $redo, $email, $reset);
self.$sidebar.find('.sidebar-item').wrap('<li/>');
self.$sidebar.appendTo("body");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定问题是什么 - 我的解释是您正在寻求重构建议。如果这就是您正在寻找的,这就是我可能实现相同要求的方式:
注意:
e.preventDefault()
通常应该是处理程序的第一行,这样如果出现异常稍后抛出,锚点仍然不会导航(更容易调试);e
事件对象之外没有任何变量 - 对我来说,这是一个比创建self
引用和一堆集合对象更优雅的解决方案(这只是我的意见)。希望这有帮助!
I'm not really sure what the question is - my interpretation is that you're asking for refactoring advice. If that's what you're looking for, here's the way I'd probably implement the same requirements:
Notes:
e.preventDefault()
should generally be the first line of your handler, that way if an exception is thrown later, the anchor will still not navigate (easier to debug);e
event object - to me, this is a more elegant solution than creating aself
reference and a bunch of collection objects (that's just my opinion).Hope this helps!