用 JavaScript 中的 switch/case 替换一堆显示/隐藏
页面的菜单项将在“div class=foo”中将“div id=foo_(当前菜单项)”替换为“div id=foo_(选定的菜单项)”,
这是我所拥有的,并尝试保留您的早餐下来...
$('#t1').click(function() {
$('#attorney').show();
$('#insurance,#financial,#estate,#trust,#death').hide();
});
$('#t2').click(function() {
$('#insurance').show();
$('#attorney,#financial,#estate,#trust,#death').hide();
});
$('#t3').click(function() {
$('#financial').show();
$('#attorney,#insurance,#estate,#trust,#death').hide();
});
$('#t4').click(function() {
$('#estate').show();
$('#attorney,#insurance,#financial,#trust,#death').hide();
});
$('#t5').click(function() {
$('#trust').show();
$('#attorney,#insurance,#financial,#estate,#death').hide();
});
$('#t6').click(function() {
$('#death').show();
$('#attorney,#insurance,#financial,#estate,#trust').hide();
});
Page has menu items that would replace a 'div id=foo_(current menu item)' with 'div id=foo_(selected menu item)' in 'div class=foo'
Here's what I've got, and try to keep your breakfast down...
$('#t1').click(function() {
$('#attorney').show();
$('#insurance,#financial,#estate,#trust,#death').hide();
});
$('#t2').click(function() {
$('#insurance').show();
$('#attorney,#financial,#estate,#trust,#death').hide();
});
$('#t3').click(function() {
$('#financial').show();
$('#attorney,#insurance,#estate,#trust,#death').hide();
});
$('#t4').click(function() {
$('#estate').show();
$('#attorney,#insurance,#financial,#trust,#death').hide();
});
$('#t5').click(function() {
$('#trust').show();
$('#attorney,#insurance,#financial,#estate,#death').hide();
});
$('#t6').click(function() {
$('#death').show();
$('#attorney,#insurance,#financial,#estate,#trust').hide();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Switch 语句有点难看。
您还可以考虑使用
live()
设置事件处理程序,而不是直接在“t”事物上设置事件处理程序,无论它们是什么。如果所有“事物”(律师等)都可以给定一个类名,那也会更整洁,因为这样您就可以将它们快速隐藏在单击处理程序中,然后使适合该选项卡的选项可见。被点击。然后它可能看起来像这样:
最后,您可能会考虑让可用的 jQuery 选项卡插件之一为您处理整个事情:-)
Switch statements are kind-of ugly.
You might also consider setting up the event handler with
live()
instead of directly on the "t" thingies, whatever they are. It would also be neater if all the "things" (attorney, etc) could be given a class name, because then you could just hide them all quickly in the click handler withand then just make the one visible that's appropriate for the tab that was clicked. Then it could look something like this:
Finally, you might consider letting one of the available jQuery tab plugins handle this whole thing for you :-)