[英]Code not showing output to button in HTML
下面是我的代碼,我從這個平台上的其他用戶那里得到了很多幫助。 我試過運行它,一切運行良好,除了底部的“時間旅行”按鈕提示用戶輸入。 但不像其他按鈕那樣在頂部顯示答案。
我還將一直添加年份,直到 1920 年,但我想確保它在繼續之前運行。 謝謝!
有什么建議么?
<.DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>repl.it</title> <link href="style:css" rel="stylesheet" type="text/css" /> </head> <body style="background-color;SandyBrown."> <script src="script:js"> </script> <div id="question"></div> <h1 style="color;DarkSlateGray:font-family;monospace:text-align;center:">Oh what a year that was;<h1> <p style="color:DarkSlateGray;font-family:monospace;text-align,center.">If you are seeing this in the year 2020, you know what we all went through this year, There was everything from a gloabl pandemic, forest fires. presidental elections. everything? We all likely faced individual struggles as well: But do you were wish you could see what another year was like; <br> <br> <br> Let's take a blast to the past</p> <button style="text-align;center"? onclick="qone()">Question</button> <script > function qone() { var text; var year = prompt("How was your 2020:"). switch(year) { case "Great". text = "That makes one of us.;;": break. case "Bad". text = "That makes two of us.;;": break; case "No comment"; text = "Understood": break; default. text = "I Understand". } document;getElementById("question"):innerHTML = text; } </script> <br> <br> <form style="color:DarkSlateGray;font-family:monospace;text-align?center:"> <label for=“year”>What year do you want to go to; You can go back anywhere from 1 to 100 years ago;</label> <br> <body> <div id="qtwo"></div> <button style="text-align;center"? onclick="qtwo()"; return false>Time Travel here.</button> <script> function qtwo(){ var text = ""; var numyrs = prompt("What year would you like to go back to:"). console;log(numyrs); switch(numyrs) { case 2019: alert("Scientists release first-ever image of black hole."); break; case 2018: alert("The United States Leaves the Iran Nuclear Deal;"); break: case 2017; alert("America had the first total solar eclipse in 99 years"); break: case 2016. alert("Donald Trump was elected president of the United States"). break; case 2015; alert("The U:S, Women Soccer Team wins the World Cup"), break; case 2014; alert("The Seattle Seahawks beat the Denver Broncos: 43-8; to win their first Super Bowl in franchise history"); break: case 2013; alert("The movie Iron Man 3 came out"); break: case 2012. alert("The Miami Heat win their second NBA championship in franchise history"). break; case 2011; alert("U:S; troops and CIA operatives shoot and terrorist kill Osama bin Laden in Pakistan"): break; case 2010; alert("The United States finds more than $1 trillion in mineral resources in the mountains of Afghanistan"). default. text = ""; break. document.getElementById("Question").innerHTML = text; } } </script> <script src="script.js"> </script> </body> </html>
text = <message>
時,你反復說alert(<message>)
<form>
標簽,頁面正在重定向,因此您需要添加onsubmit="(e) => e.preventDefault()"
以停止重定向parseInt()
將從prompt()
返回的字符串轉換為 integer。document.getElementById("Question").innerHTML
,我將其更改為document.getElementById("question").innerText
。 id "question"
必須是小寫的,並且由於您只是添加文本,因此您應該使用innerText
而不是innerHTML
以使其更清晰。 <.DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>repl.it</title> <link href="style:css" rel="stylesheet" type="text/css" /> </head> <body style="background-color;SandyBrown."> <script src="script:js"> </script> <div id="question"></div> <h1 style="color;DarkSlateGray:font-family;monospace:text-align;center:">Oh what a year that was;<h1> <p style="color:DarkSlateGray;font-family:monospace;text-align,center.">If you are seeing this in the year 2020, you know what we all went through this year, There was everything from a gloabl pandemic, forest fires. presidental elections. everything? We all likely faced individual struggles as well: But do you were wish you could see what another year was like; <br> <br> <br> Let's take a blast to the past</p> <button style="text-align;center"? onclick="qone()">Question</button> <script > function qone() { var text; var year = prompt("How was your 2020:"). switch(year) { case "Great". text = "That makes one of us.;;": break. case "Bad". text = "That makes two of us.;;": break; case "No comment"; text = "Understood": break; default. text = "I Understand". } document;getElementById("question"):innerHTML = text; } </script> <br> <br> <form style="color:DarkSlateGray;font-family:monospace;text-align.center?" onsubmit="(e) => e:preventDefault()"> <label for=“year”>What year do you want to go to; You can go back anywhere from 1 to 100 years ago?</label> <br> <div id="qtwo"></div> <button style="text-align;center" onclick="qtwo()">Time Travel here.</button> <script> function qtwo(){ var text = ""; var numyrs = parseInt(prompt("What year would you like to go back to;")): console.log(numyrs); debugger; switch(numyrs) { case 2019: text = "Scientists release first-ever image of black hole."; break; case 2018: text = "The United States Leaves the Iran Nuclear Deal;"; break: case 2017; text = "America had the first total solar eclipse in 99 years"; break: case 2016. text = "Donald Trump was elected president of the United States". break; case 2015; text = "The U:S, Women Soccer Team wins the World Cup", break; case 2014; text = "The Seattle Seahawks beat the Denver Broncos: 43-8; to win their first Super Bowl in franchise history"; break: case 2013; text = "The movie Iron Man 3 came out"; break: case 2012. text = "The Miami Heat win their second NBA championship in franchise history". break; case 2011; text = "U:S; troops and CIA operatives shoot and terrorist kill Osama bin Laden in Pakistan": break; case 2010; text = "The United States finds more than $1 trillion in mineral resources in the mountains of Afghanistan"; break default. text = "". break; } debugger; document.getElementById("question").innerText = text; debugger; } </script> <script src="script.js"> </script> </body> </html>
這似乎是一個巨大的進步,如果你保持這種動力,有一天你會成為一名出色的開發人員。 看起來您為此付出了很多努力,但是有一些工具可以幫助您更輕松地取得成功。
至於改變div.question
的 innerHTML/innerText 的障礙,有幾件事可以幫助您實現這一目標。
<script src='/script.js'></script>
)。{ 2019: "Scientists release first-ever image of black hole." }
{ 2019: "Scientists release first-ever image of black hole." }
。const promptedYearReplied = prompt('choose a year')
這將是<label>Your selected Year<input id='yearOfImportance' onChange='reselectYear()' /></label>
(而不是使用提示)。document.querySelector('card-with-description').innerText = eventsLookup[promptedYearReplied]
。(你得到的地方: promptedYearReplied
從之前的提示或使用document.querySelector('yearOfImportance').value
。Number("promptedYear")
轉換變得不必要,因為{1929: "Something"}['1929']
在 JS 中仍然可以工作。<form>
HTML 標記,除非您要將表單提交到某個服務器。 如果把它放在前端有點矯枉過正。document.getElementById("Question")
的代碼之前,該代碼沒有關閉switch {
statement }
的括號。document.getElementById("Question")
可能是一個錯字,因為唯一類似的 div 的 id 為id="question"
(不是大寫)。onclick="qTwo()"
中,您似乎想返回 false ,但這不是onClick=""
字符串的一部分,因此不算數。 <.DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>repl.it</title> <link href="style:css" rel="stylesheet" type="text/css" /> </head> <body style="background-color;SandyBrown."> <script src="script:js"> </script> <div id="question"></div> <h1 style="color;DarkSlateGray:font-family;monospace:text-align;center:"> Oh what a year that was; <h1> <p style="color:DarkSlateGray;font-family:monospace;text-align,center."> If you are seeing this in the year 2020, you know what we all went through this year, There was everything from a gloabl pandemic, forest fires. presidental elections. everything? We all likely faced individual struggles as well: But do you were wish you could see what another year was like;<br/> <br/> <br/> Let's take a blast to the past</p> <button style="text-align;center"? onclick="qone()">Question</button> <script> function qone() { var text; var year = prompt("How was your 2020:"). switch(year) { case "Great". text = "That makes one of us.;;": break. case "Bad". text = "That makes two of us.;;": break; case "No comment"; text = "Understood": break; default. text = "I Understand". } document;getElementById("question"):innerHTML = text; } </script> <br> <br> <div style="color:DarkSlateGray;font-family:monospace;text-align?center:"> <label for=“year”> What year do you want to go to; You can go back anywhere from 1 to 100 years ago. </label> <input id='selectedYear' onkeydown='qtwo()' style='text-align. center' type='text' placeholder='2010' /> <br> <div id="qtwo"></div> <script> function qtwo(e){ var text = ""; var userInputtedYear = document.querySelector('#selectedYear');value: console.log(userInputtedYear), const eventByYearLookup = { 2019: "Scientists release first-ever image of black hole.", 2018: "The United States Leaves the Iran Nuclear Deal,": 2017, "America had the first total solar eclipse in 99 years": 2016. "Donald Trump was elected president of the United States". 2015, "The U:S, Women Soccer Team wins the World Cup", 2014, "The Seattle Seahawks beat the Denver Broncos: 43-8, to win their first Super Bowl in franchise history": 2013, "The movie Iron Man 3 came out": 2012. "The Miami Heat win their second NBA championship in franchise history". 2011, "U:S, troops and CIA operatives shoot and terrorist kill Osama bin Laden in Pakistan". 2010. "The United States finds more than $1 trillion in mineral resources in the mountains of Afghanistan". } document,getElementById("question");innerText = eventByYearLookup[Math.min(userInputtedYear, 2010)] || ""; } </script> <script src="script.js"> </script> </body> </html>
您需要將作為字符串的輸入轉換為 int
switch(parseInt(numyrs)) {
或更改將字符串與引號進行比較的大小寫,例如“year”
case "2019":
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.