gpt4 book ai didi

javascript - 打开页面就像点击导航元素一样?

转载 作者:行者123 更新时间:2023-11-30 17:33:59 26 4
gpt4 key购买 nike

我有一个单页站点,它根据单击的导航元素隐藏和显示元素。谁能建议如何打开此页面,就好像已经单击了其中一个元素一样?我正在尝试在其他地方放置一个链接,该链接将打开页面并立即显示“演出”元素。

网站是 www.brookebentham.co.uk

最佳答案

使用哈希

http://www.site.com/#tab/firstTab

然后在页面加载或onhashchange event , 查看哈希并根据它做一些事情

$(document).ready(hashChanged);
window.onhashchange = hashChanged;

function hashChanged() {
var hash = window.location.hash;
var tab = hash.replace("tab/","");
$("#"+tab).show();
}

或者您可以使用 history.pushState (如果支持)并检查路径

//assume something like as url http://www.site.com/tab/firstTab

//history has changed (ie on using pushSate)
window.onpopstate = function(){
//path will be "/tab/firstTab"
var path = window.location.pathname;
var tab = path.replace("/tab/","");
$("#"+tab).show();
};

$("#firstTab").click(function(e){
e.preventDefault();
history.pushState({},"Title (not really used","/tab/firstTab");
});

要充分利用 pushState,您需要后端进行 url 重写,例如 apache 的 mod_rewrite 将所有 url 指向您的一页。

关于javascript - 打开页面就像点击导航元素一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22422699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com