gpt4 book ai didi

javascript - 链接到网站中被隐藏/显示 JavaScript 函数隐藏的部分

转载 作者:行者123 更新时间:2023-11-28 02:55:39 27 4
gpt4 key购买 nike

我使用一些 JavaScript 在单击选项卡时显示/隐藏网站的部分。我试图弄清楚是否有一种方法可以链接回该页面并根据该链接打开某个选项卡。

这是 JS:

   var ids=new Array('section1','section2','section3','section4');

function switchid(id, el){
hideallids();
showdiv(id);

var li = el.parentNode.parentNode.childNodes[0];
while (li) {
if (!li.tagName || li.tagName.toLowerCase() != "li")
li = li.nextSibling; // skip the text node
if (li) {
li.className = "";
li = li.nextSibling;
}
}
el.parentNode.className = "active";
}

function hideallids(){
//loop through the array and hide each element by id
for (var i=0;i<ids.length;i++){
hidediv(ids[i]);
}
}

function hidediv(id) {
//safe function to hide an element with a specified id
document.getElementById(id).style.display = 'none';
}

function showdiv(id) {
//safe function to show an element with a specified id
document.getElementById(id).style.display = 'block';
}

和 HTML

<ul>
<li class="active"><a onclick="switchid('section1', this);return false;">One</a></li>
<li><a onclick="switchid('section2', this);return false;">Two</a></li>
<li><a onclick="switchid('section3', this);return false;">Three</a></li>
<li><a onclick="switchid('section4', this);return false;">Four</a></li>
</ul>

<div id="section1" style="display:block;">
<div id="section2" style="display:none;">
<div id="section3" style="display:none;">
<div id="section4" style="display:none;">

我无法想出一种方法来链接回特定部分。这个方法还可以吗?

谢谢!

最佳答案

您可以在页面加载时运行一些脚本来检查 url 哈希并加载相应的部分:

// on page load
var sectionid = /section\d/i.exec(location.hash);
if (sectionid) {
var link = document.getElementById(switchid[0] +"_link");
switchid(sectionid[0], link);
}

&向您的链接添加 ID:

<li><a id="section2_link" onclick="switchid('section2', this);return false;">Two</a></li>

关于javascript - 链接到网站中被隐藏/显示 JavaScript 函数隐藏的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2740753/

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