Rails 3 干而不引人注目的 javascript
我正在将所有内容更改为不显眼的 javascript,因为我认为它使事情更容易调试等。
我有类似的东西:
$(document).ready(function(){
$('#photos_link').click(function()
{
select_tab('main', 'photos', $(this));
});
$('#dealership_link').click(function()
{
select_tab('main', 'dealership', $(this));
});
$('#dealership').hide();
});
如你所见,我有 2 个非常相似的构造,我想将它们干燥,任何想法?
I am changing everything to unobtrusive javascript, since I think it makes things much easier to debug etc.
I've got something like:
$(document).ready(function(){
$('#photos_link').click(function()
{
select_tab('main', 'photos', $(this));
});
$('#dealership_link').click(function()
{
select_tab('main', 'dealership', $(this));
});
$('#dealership').hide();
});
as you can see I've got 2 very similar constructs, I'd like to dry them out, any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有两个纯 JS 重构:
我
个人更喜欢前者。
Here are two pure-JS refactors:
and
I'd personally prefer the former.
在 ApplicationHelper (或适当的帮助器)中创建一个帮助器方法:
然后,从您的视图中调用它:
Create a helper method in ApplicationHelper (or the appropriate helper):
Then, call it from your view: