![](/img/trans.png)
[英]how the browser execute this javascript code: window.location.href=“url”
[英]jQuery on window.location.href execute this code
我需要从home.html转到services.html,类.services_profile是一个子菜单,需要转到services.html并激活.services_profile_tab
$(".services_profile").click(function(e){
$('.nav-tabs li a.tab-profile').attr("aria-expanded","true").parent().siblings().removeClass('active');
$('.nav-tabs li a.tab-profile').parent().addClass('active');
$('.tab-content').children().removeClass('in active');
$('.tab-content .profile').addClass('in active');
setTimeout(function() {
window.location.href = 'services.html';
},1000);
// This does not work
if (window.location.pathName.indexOf('services') >= 0) {
$('services_profile_tab').addclass('active');
}
});
路径名应在中为小写:
if (window.location.pathName.indexOf('services') >= 0) {
而且您还缺少一个点.
中的services_profile_tab
之前的类的类别:
$('services_profile_tab').addclass('active');
希望这可以帮助。
您需要将选项卡引用传递到服务页面,或者在url
,也可以使用window.localStorage
来设置特定的选项卡,使其在导航到特定页面时处于活动状态,例如在URL中使用带有散列的ref传递引用:
$(".services_profile").click(function(e){
$('.nav-tabs li a.tab-profile').attr("aria-expanded","true")
.parent().siblings().removeClass('active');
$('.nav-tabs li a.tab-profile').parent().addClass('active');
$('.tab-content').children().removeClass('in active');
$('.tab-content .profile').addClass('in active');
setTimeout(function() {
window.location.href = 'services.html#services_profile_tab';// pass the tab reference
},1000);
});
现在在doc ready区块的服务页面上:
$(function(){
if (window.location.indexOf('services') >= 0) {
var tab = window.location.hash.substr(1);
$('.'+tab).addclass('active');
}
});
您可以执行以下操作来动态传递选项卡引用:
window.location.href = this.className.split('_')[0]'.html#'+this.className+'_tab';
// so it results in
// services.html#services_profile_tab => when "services_profile" clicked
这里this.className
是要导航的单击元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.