在CSS中,可以使用location.replace()
或location.assign()
方法來實(shí)現(xiàn)網(wǎng)頁跳轉(zhuǎn)到另一個(gè)網(wǎng)頁的功能。
location.replace()
方法會(huì)替換當(dāng)前網(wǎng)頁,而location.assign()
方法會(huì)在瀏覽器歷史記錄中添加一條記錄。
假設(shè)你想要將網(wǎng)頁跳轉(zhuǎn)到https://html4.cn
,可以使用以下CSS代碼:
location.replace('https://html4.cn');
或者:
location.assign('https://html4.cn');
需要注意的是,這些代碼應(yīng)該被包含在JavaScript中,而不是CSS中,你需要確保你的網(wǎng)頁中包含有JavaScript代碼,或者將CSS代碼轉(zhuǎn)換為JavaScript代碼。
如果你想要在網(wǎng)頁中創(chuàng)建一個(gè)鏈接,讓用戶點(diǎn)擊后跳轉(zhuǎn)到另一個(gè)網(wǎng)頁,可以使用HTML中的<a>
標(biāo)簽。
<a href="https://html4.cn">點(diǎn)擊這里跳轉(zhuǎn)到另一個(gè)網(wǎng)頁</a>
這種方法會(huì)在瀏覽器中創(chuàng)建一個(gè)新的歷史記錄,用戶可以通過點(diǎn)擊“返回”按鈕來回到原來的網(wǎng)頁。