gpt4 book ai didi

javascript - 使用包含相关的html页面时如何更改url

转载 作者:太空宇宙 更新时间:2023-11-04 07:50:15 25 4
gpt4 key购买 nike

如何更改 url 路径,同时在内部选项卡容器中的 javascript 中使用 include html 方法。我在我的网站中使用单页布局,并使用选项卡样式,每个选项卡都有自己的带有 include 方法的 html 页面。

我附上了我的示例文件,请建议如何在不刷新页面的情况下更改 url 路径。

function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();


// page load
document.getElementById("Submenu1").innerHTML='<object type="text/html" data="submenu1.html" ></object>';
document.getElementById("Submenu2").innerHTML='<object type="text/html" data="submenu2.html" ></object>';
document.getElementById("Submenu3").innerHTML='<object type="text/html" data="submenu3.html" ></object>';
/* Style the tab */
.tab {
float: left;
border: 1px solid #ccc;
background-color: #f1f1f1;
width: 30%;
height: 300px;
}

/* Style the buttons inside the tab */
.tab button {
display: block;
background-color: inherit;
color: black;
padding: 22px 16px;
width: 100%;
border: none;
outline: none;
text-align: left;
cursor: pointer;
transition: 0.3s;
font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}

/* Create an active/current "tab button" class */
.tab button.active {
background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
float: left;
padding: 0px 12px;
border: 1px solid #ccc;
width: 70%;
border-left: none;
height: 300px;
}
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Submenu1')" id="defaultOpen">SubMenu 1</button>
<button class="tablinks" onclick="openCity(event, 'Submenu2')">SubMenu 2</button>
<button class="tablinks" onclick="openCity(event, 'Submenu3')">SubMenu 3</button>
</div>

<div id="Submenu1" class="tabcontent">
<h1>external submenu1.html page here</h1>
</div>

<div id="Submenu2" class="tabcontent">
<h1>external submenu2.html page here</h1>
</div>

<div id="Submenu3" class="tabcontent">
<h1>external submenu3.html page here</h1>
</div>

我希望,会理解我的请求,抱歉英语不好。

最佳答案

你可以使用 window.location.hash = "#"+cityName; 如果你想在不刷新页面的情况下更改 url。

function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
window.location.hash = "#"+cityName;

}

关于javascript - 使用包含相关的html页面时如何更改url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47827344/

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