簡體   English   中英

javascript 中的 window.location.href 只是重新加載頁面,而不是導航到另一個網站

[英]window.location.href in javascript just reloading page, not navigating to another website

我想使用按鈕從我的網站導航到另一個網站,這是我的代碼,我在這個項目中使用 React Bootstrap

<button onclick="window.location.href = 'https://www.glassdoor.com';">Click Here</button>

但是,當我單擊它時,它不會導航到新網站,它只會重新加載我所在網站的主頁。

window.location.href返回當前頁面的href(URL)

因此,您可以刪除href部分並簡單地設置您希望導航到的 url 的值。

 <button onclick="window.location = 'https://www.glassdoor.com'">Click Here</button>

如果失敗,您也可以使用 function assign

 <button onclick="window.location.assign('https://www.glassdoor.com')">Click Here</button>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM