我有兩個 html 頁面:index1.html 和 index2.html。在 index1.html 中,我有兩個按鈕:開始和登錄。在 index2.html 中,我有兩個 div(注冊表單和登錄表單),其中一個是活動的(通過使用活動類設定它們)。默認活動是注冊表單:<div >...</form>,而另一個用于登錄的表單尚未設定為活動:<div >...</div>
請注意,index2.html 是一個雙滑塊頁面,它不會向下滾動,因為 div 彼此并排放置。
我希望 index1.html 中的“入門”按鈕重定向到 index2.html,但在活動的注冊表單上,并且我希望 index1.html 中的“登錄”按鈕重定向到 index2.html,但在活動的登錄上形式。
問題是我無法將它們重定向到特定的活動 div。index2.html 的 URL 僅與“index2.html?#”一起使用。如何根據活動 div 更改 URL?前任。index2.html#sign-up(如果注冊表單處于活動狀態)和 index2.html?#sign-in(如果登錄表單處于活動狀態)。
uj5u.com熱心網友回復:
我已嘗試對其進行模擬和實施解決方案,請查看https://stackblitz.com/edit/web-platform-uawtkc?file=login.html,希望對您有所幫助。
基本上,當從一個檔案導航到另一個檔案時,您需要一些 JS 來標記何時要激活哪個元素。
uj5u.com熱心網友回復:
在 index1.html 中,您可以在 href 中使用主題標簽
<a href="index2.html#sign-in"><div>Sign in</div></a>
在 index2.html 中,您可以檢查 url 中是否有登錄主題標簽
const signInForm = document.getElementById("sign-in-form")
const currentHash = window.location.hash
if(currentHash.includes('sign-in')){
signInForm.classList.add("active")
signUpForm.classList.remove("active")
}
(請注意,我在您的登錄和注冊按鈕中輸入了 id)
<div id="sign-up-form" class="form sign-up-form active">Sign Up</div>
<div id="sign-in-form" class="form sign-in-form">Sign in</div>
為了解釋,location.hash 屬性設定或回傳 URL 的錨部分,包括井號 (#)。然后在我們從 url 中獲取主題標簽后,我們可以檢查我們的主題標簽中是否有“登錄”字樣
uj5u.com熱心網友回復:
如果你想讓url跟隨你的元素類,你必須先獲取元素,然后獲取它們的類,然后檢查哪個有活動類
<div id="sign-up-form" class="form sign-up-form active">Sign Up</div>
<div id="sign-in-form" class="form sign-in-form">Sign in</div>
獲取他們的課程
const signInForm = document.getElementById("sign-in-form").classList
const signUpForm = document.getElementById("sign-up-form").classList
檢查哪個是活動的
if (signInForm.contains("active")) window.location.hash = "SignIn"
if (signUpForm.contains("active")) window.location.hash = "SignUp"
您可以使用此 window.location.hash = "new word" 更改 url 主題標簽
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429440.html
標籤:javascript html jQuery 重定向
上一篇:為什么谷歌瀏覽器會自動將`http://app`重定向到`https://app`而不會重定向到`http://app2`或`http://napp`?
