<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
a {
display: inline-block;
text-decoration: none;
color: black;
width: 100px;
height: 50px;
background-color: orangered;
text-align: center;
line-height: 50px;
border-radius: 5px;
font-size: 22px;
}
</style>
</head>
<body>
<p></p>
<a href="#/a">a</a>
<a href="#/b">b</a>
<script>
window.onload = function () {
location.hash === '#/a' && (document.querySelectorAll('p')[0].innerText = 'a');
location.hash === '#/b' && (document.querySelectorAll('p')[0].innerText = 'b');
}
window.onhashchange = function (evt) {
location.hash === '#/a' && (document.querySelectorAll('p')[0].innerText = 'a');
location.hash === '#/b' && (document.querySelectorAll('p')[0].innerText = 'b');
}
</script>
</body>
</html>
前端hash路由的实现
最新推荐文章于 2023-08-16 17:17:49 发布