我在页面上有一个选项卡式设置,我想自动突出显示相应的菜单选项卡以及根据 # 哈希显示相应的内容 div。
示例:
http://design.vitalbmx.com/user_menu/member_profile_so.html -- 无哈希,打开第一个选项卡
http://design.vitalbmx.com/user_menu/member_profile_so.html#setup -- #setup,应该打开“设置”选项卡
正如您所看到的,它适用于突出显示“设置”选项卡。但内容 div 不会改变。
脚本如下:
var tab_content_current = 1; switch (window.location.hash) { case '#activity': tab_content_current = 1; break; case '#friends': tab_content_current = 2; break; case '#photos': tab_content_current = 3; break; case '#videos': tab_content_current = 4; break; case '#setup': tab_content_current = 5; break; case '#forum': tab_content_current = 6; break; case '#blog': tab_content_current = 7; break; case '#comments': tab_content_current = 8; break; case '#favorites': tab_content_current = 9; break; case '#profile-comments': tab_content_current = 10; break; default: tab_content_current = 1; } if (tab_content_current != 1) { change_active_tab (tab_content_current); } function tabs_toggle (id) { if (id != tab_content_current) { change_active_tab (id); tab_content_current = id; } } function change_active_tab (id) { $j('.profile_tabs li').removeClass('active'); if (id < 8) $j('.profile_tab_'+id).addClass('active'); $j('.profile_content').hide(); $j('#profile_content_'+id).fadeIn(); }
请注意,当您实际单击菜单选项卡时它会起作用。
任何解决此问题的帮助将不胜感激。
最佳答案
将脚本移至页面最底部、profile_content div 后面。这样,它们就会在脚本运行之前位于 DOM 中。 It is also best to put scripts at the bottom of the page for speed reasons 。
关于javascript - jQuery:函数的一部分未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2968028/