直接コーディング
<p><a href="#/home">首页</a>|<a href="#/about">关于</a>|<a href="#/info">信息</a></p>
<div id="home">
<h1>首页</h1>
</div>
<div id="about">
<h1>关于</h1>
</div>
<div id="info">
<h1>信息</h1>
</div>
- 最初のものはデフォルトで表示されます、それはホームページです
<style>
#about {
display: none;
}
#info {
display: none;
}
</style>
- ページ効果を見てください
- 次に、要件を達成する必要があります。オンこのページは、同時に、同時にURL対応するタグも表示hrefコンテンツ
- 沿ってhashchangeこの要件を満たすようにイベントを変更する
- ハッシュページに表示されているURL
- たくさんもし判断は対応するテキストを表示させることです
- Document.getElementByIdは、要素がページ上の唯一の要素である場合に直接使用できるため、使用されません。
window.onhashchange = function (e) {
let hash = window.location.hash;
console.log(hash)
if (hash == '#/home') {
home.style.display = 'block';
about.style.display = 'none';
info.style.display = 'none';
} else if (hash == '#/about') {
home.style.display = 'none';
about.style.display = 'block';
info.style.display = 'none';
} else if (hash == '#/info') {
home.style.display = 'none';
about.style.display = 'none';
info.style.display = 'block';
}
}
Vueルーティングジャンプ(ポジティブプレイ)
- 親しみを感じますか?
- これは、ルーティング用にvueを構成する方法です
- 道アドレスバーに表示されますURL
- 成分表示する必要がありますページ、ここにそれらのdivがあります
const router = [
{
path:'#/home',component:home},
{
path:'#/about',component:about},
{
path:'#/info',component:info}
]
let currentView = router[0];
window.onhashchange = function(e) {
for(let i = 0; i < router.length; i++) {
if(location.hash == router[i].path) {
currentView.component.style.display = 'none';
router[i].component.style.display = 'block';
currentView = router[i];
break;
}
}
}