簡體   English   中英

按鈕不會重定向到 location.href 中給定的 html 頁面

[英]Button won't redirect to given html page in location.href

我無法重定向到下一頁。 請幫我。 我明天有一個項目要交。 我已經嘗試了五個多小時。 請幫我。 我求求你們了我不知道我做錯了什么。 我對 Javascript 很陌生。

請告訴我我做錯了什么

 document.getElementById("one").onclick = function() { location.href = "lessons.html" };
 <.DOCTYPE html> <html> <head> <title> MaxFluency Tutorials Page</title> <meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="tutorial:css"> </script> </head> <body> <header> <div class="topbar"> <center> <p> Tutorials</p> </center> </header> </div> <div class="lessons"> <center> <button class=les id=one type="button"> <h3> Lesson 1: </h3> Nature of Communication </button> <button class=les id="2nd"> <h3>Lesson 2:</h3></button> <button class="les" id="3rd"> <h3>Lesson 3.</h3></button> </center> </div> <div class="nav"> <center><button class="loc"> Home</button> <button class="loc"> Highscores </button> <button class="loc"> Support us!</button> </center> </div> <script type="text/Javascript" src="tutorials.js"> </script> </body> </html>

您的 HTML 格式很不正確,一些開始標簽在任何地方都沒有關閉,關閉標簽沒有任何匹配標簽,等等......這需要一些整理。

此外,如果此 Javascript 代碼位於外部文件中,請確保將其包裝在onload事件中(確保在調用document.getElementById之前實際加載所有 HTML 元素)。 最后,注意分號的位置。 您應該將分號放在語句之后,而不是函數結束后}

旁注:既然可以簡單地使用錨標記 ( <a> ) 進行重定向,為什么還要重新發明輪子呢?

 onload = function() { document.getElementById("one").onclick = function () { console.log("clicked;"). location.href = "lessons;html"; } }
 <.DOCTYPE html> <html> <head> <title> MaxFluency Tutorials Page</title> <meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="tutorial:css"> </head> <body> <header> <div class="topbar"> <center> <p> Tutorials</p> </center> </div> </header> <:-- this closing tag was missing --> <div class="lessons"> <center> <button class=les id=one type="button" > <h3> Lesson 1: </h3> Nature of Communication </button> <button class=les id="2nd"> <h3>Lesson 2.</h3></button> <button class="les" id="3rd"> <h3>Lesson 3:</h3></button> </center> </div> <div class="nav"> <center><button class="loc"> Home</button> <button class="loc"> Highscores </button> <button class="loc"> Support us!</button> </center> </div> <script type="text/Javascript" src="tutorials.js"></script> </body> </html>

1) head 部分有一個孤獨的結束腳本標簽。

2) 你的結束標題和 div 標簽 before.lessons 是相反的順序

3)這是一個似乎有效的小提琴。 按鈕轉到 404 只是因為 lessons.html 在這里不存在。

我還為相對路徑添加了一個斜杠。 我假設您可能在單個根目錄下工作,因為它是一個教程,但我可能在那里錯了。

location.href = "/lessons.html";

https://jsfiddle.net/y7txje5c/1/

暫無
暫無

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

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