在window.location.href上执行以下代码
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
是被点击来导航的元素
相关文章:
- setTimeout函数能否在其前面的代码执行之前激发
- 延迟代码执行,直到下载完成
- 奇怪的javascript代码执行顺序
- 将字符串作为一段 HTML/javascript 代码执行
- 使用AngularJS,如何在视图和控制器同步特定更改后触发代码执行
- onKeyUp 导致代码执行两次
- JQuery:将参数传递给插件以限制代码执行
- JavaScript代码执行时间
- 基本的JavaScript代码执行
- JavascriptCore:从本机代码执行JavaScript定义的回调函数
- 脚本标记的位置会影响代码执行
- 页面脚本未使用 Salesforce Web-to-Lead 代码执行
- GM_xmlhttpRequest 成功回调“onload”未为注入的代码执行
- 停止所有代码执行,直到 IFrame 完全加载
- 纯 Javascript 代码执行停止混合应用程序中的离子加载指示器
- Javascript代码执行顺序
- Javascript:在嵌套函数中使用条件语句停止代码执行
- Node.js代码执行顺序
- 什么触发JavaScript代码执行
- onreadystatechange导致php代码执行两次