2016-11-15 178 views
-1

只有腳本上方的html文本才顯示出來;提示不會出現。我無法弄清楚什麼是遺漏/錯誤。也是爲了做家庭作業,我沒有編寫主題/內容哈哈。爲什麼我的javascript不能工作?

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8"> 
     <title>Part 1 Problem 6</title> 
    </head> 
    <body> 
     <h1>JS Lab 1 Question Answers</h1> 
     <ol> 
      <li>document.write is the script that tells the browser to write certain text. document.getElementById allows you to change certain text that is already written by calling it by its id. Using document.getElementById, you can modify a large group of your code at the same time.</li> 
      <li>A variable holds the place of a certain value in your code, like a number or a name. An array holds multiple values</li> 
      <li>If there was no variable with the prompt, you would still get the prompt but nothing would be done about it. What you enter into the prompt becomes the value of the variable.</li> 
      <!--<script>document.write("<li>To generate a random number between 50 and 350 (not including 350) you would write var randnum (or whatever you name your variable) = (Math.floor(Math.random() * 300) + 50)</li>")</script>--> 
      <li>If you generate 4 different random numbers, you should have at least 4 variables.</li> 
     </ol> 
     <script> 
      var insultarray = new Array(); 
      insultarray[0] = "You smell bad"; 
      insultarray[1] = "You are ugly"; 
      insultarray[2] = "I just really don't like you"; 
      insultarray[3] = "You are annoying"; 
      insultarray[4] = "Your hair looks bad"; 
      var randnum = (Math.floor(Math.random()*5); 
      var x = prompt("Want to know what I think?","yes"); 
      if (x == "yes") 
      { 
       document.write("<p>" + insultarray[randnum] + "</p>"); 
      } 
      else 
      { 
       document.write("<p>Honestly, I like you.</p>"); 
      } 
     </script> 
    </body> 
</html> 
+1

在瀏覽器開發人員工具控制檯中是否有任何錯誤? –

+0

'var randnum =(Math.floor(Math.random()* 5);'< - 缺少右括號 – adeneo

+3

'var randnum =(Math.floor(Math.random()* 5);'Your missing another') '檢查你的瀏覽器控制檯是否有錯誤。另外,document.write並不是真正理解DOM的理想方式。 – Keith

回答

0

您有一個Math.floor(Math.random()*5)的領導支架,我在下面刪除並正在工作。

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
     <title>Part 1 Problem 6</title> 
 
    </head> 
 
    <body> 
 
     <h1>JS Lab 1 Question Answers</h1> 
 
     <ol> 
 
      <li>document.write is the script that tells the browser to write certain text. document.getElementById allows you to change certain text that is already written by calling it by its id. Using document.getElementById, you can modify a large group of your code at the same time.</li> 
 
      <li>A variable holds the place of a certain value in your code, like a number or a name. An array holds multiple values</li> 
 
      <li>If there was no variable with the prompt, you would still get the prompt but nothing would be done about it. What you enter into the prompt becomes the value of the variable.</li> 
 
      <!--<script>document.write("<li>To generate a random number between 50 and 350 (not including 350) you would write var randnum (or whatever you name your variable) = (Math.floor(Math.random() * 300) + 50)</li>")</script>--> 
 
      <li>If you generate 4 different random numbers, you should have at least 4 variables.</li> 
 
     </ol> 
 
     <script> 
 
      var insultarray = new Array(); 
 
      insultarray[0] = "You smell bad"; 
 
      insultarray[1] = "You are ugly"; 
 
      insultarray[2] = "I just really don't like you"; 
 
      insultarray[3] = "You are annoying"; 
 
      insultarray[4] = "Your hair looks bad"; 
 
      var randnum = Math.floor(Math.random()*5); 
 
      var x = prompt("Want to know what I think?","yes"); 
 
      if (x == "yes") 
 
      { 
 
       document.write("<p>" + insultarray[randnum] + "</p>"); 
 
      } 
 
      else 
 
      { 
 
       document.write("<p>Honestly, I like you.</p>"); 
 
      } 
 
     </script> 
 
    </body> 
 
</html>

0

你有一個失蹤支架(或一個too.much)位置:

var randnum = (Math.floor(Math.random()*5); 

它應該是這樣的:

var randnum = Math.floor(Math.random()*5); 
0
var randnum = (Math.floor(Math.random()*5); 

有是braket befo重新編號Math.floor,將其刪除。

0

笑你的腳本有一個人解釋這更像是一種傷害問題 順便說一句,你只是錯過了一個括號

var insultarray = new Array(); 
 
insultarray[0] = "You smell bad"; 
 
insultarray[1] = "You are ugly"; 
 
insultarray[2] = "I just really don't like you"; 
 
insultarray[3] = "You are annoying"; 
 
insultarray[4] = "Your hair looks bad"; 
 
var randnum = (Math.floor(Math.random()*5)); 
 
var x = prompt("Want to know what I think?","yes"); 
 
if (x == "yes") 
 
{ 
 
    document.write("<p>" + insultarray[randnum] + "</p>"); 
 
} 
 
else 
 
{ 
 
    document.write("<p>Honestly, I like you.</p>"); 
 
}
<h1>JS Lab 1 Question Answers</h1> 
 
<ol> 
 
    <li>document.write is the script that tells the browser to write certain text. document.getElementById allows you to change certain text that is already written by calling it by its id. Using document.getElementById, you can modify a large group of your code at the same time.</li> 
 
    
 
    <li>A variable holds the place of a certain value in your code, like a number or a name. An array holds multiple values</li> 
 

 
    <li>If there was no variable with the prompt, you would still get the prompt but nothing would be done about it. What you enter into the prompt becomes the value of the variable.</li> 
 

 
    <li>If you generate 4 different random numbers, you should have at least 4 variables.</li> 
 
</ol>

0

你忘了上線23添加一個右括號,它應該是:

var randnum = Math.floor(Math.random()*5);

通過查看控制檯可以發現這些錯誤。

相關問題