2015-11-09 124 views
0

我正在建立一个网站,我已经完成了,但代码中有一个很大的错误。我创建了一个表格,其中有一个按钮,它是计算某个表达式并将计算结果显示在表格中的函数。计算发生但不显示在表格中。以下是我有:一个表格中的功能按钮

<!DOCTYPE html> 
 
<html> 
 
<!--DB 11/2/2015--> 
 
<!-- W3 Schools was referenced in building this code--> 
 
<head> 
 
\t <meta charset="utf-8"> 
 
\t <title>Assignment 8</title> \t 
 
\t <link href="images/avatar.png" rel="shortcut icon" type="image/png"> 
 
\t <link href="css/stylesheet.css" rel="stylesheet" type="text/css"> 
 
\t <script src="js/javascript.js" type="text/javascript"></script> 
 
\t <style type="text/css"> 
 
\t </style> 
 
\t 
 
</head> 
 
<body onload="fillitin()"> 
 
<header> 
 
</header> 
 
<aside> 
 
</aside> 
 
<div id="main"> 
 

 
\t \t 
 
<h1> Assignment 8: Operators and Expression</h1> 
 
<br> 
 
<p id="demo"></p> \t 
 
<script> 
 

 

 
\t var d = new Date(); 
 
\t months = ['Janurary','Feburary','March','April','May','June','July','August','September','October','November','December'] 
 
\t var date = months[d.getMonth()]+" "+d.getDate()+" "+d.getFullYear(); 
 
\t 
 
\t 
 
document.getElementById("demo").innerHTML = date; 
 
</script> 
 
    
 

 

 
<h2 style="text-align:center"> S'mores!</h2> 
 

 
<h4> CLick on the button serves to learn the amount of ingredients needed to make S'mores!</h4> 
 
<table style="width:50%" border="1"> 
 
<form name="myform" method=""> 
 
<tr> 
 
\t <td> <br> 
 
    \t \t <input type="text" id="num1" value="1"> 
 
    \t </td> 
 
\t <td> 
 
\t \t <button onclick="myFunction()">Serves</button> 
 
\t </td> 
 
\t 
 
</tr> 
 
</form> 
 
<tr> 
 
\t <td id="M"></td> 
 
\t <td>Large Marshmallows</td> 
 
</tr> 
 
<tr> 
 
\t <td id="G"></td> 
 
\t <td>Graham Cracker</td> 
 
</tr> 
 
<tr> 
 
\t <td id="C"></td> 
 
\t <td>Ounches Chocolate</td> 
 
</tr> 
 

 
</table> 
 
<script> 
 
\t function fillitin() { 
 
\t document.getElementById("M").InnerHTML="1"; 
 
\t document.getElementById("G").InnerHTML="1"; 
 
\t document.getElementById("C").InnerHTML="1.5"; 
 
\t } 
 
\t 
 
\t function myFunction() { 
 
\t var x=document.getElementById("num1").value; 
 
\t document.getElementById("M").InnerHTML=x; 
 
\t document.getElementById("G").InnerHTML=x; 
 
\t document.getElementById("C").InnerHTML=x*1.5; 
 

 

 
\t } 
 
\t </script> 
 

 
<ul> 
 
<li> 
 
Heat the marshmallow over an open flame until it begins to brown and melt. 
 
</li> 
 
<li>Break the graham cracker in half. Put the chocolate on one half of the cracker, and put the warm marshmallow on the other. </li> 
 
<li>Enjoy! Don’t burn your mouth!</li> 
 
</ul> 
 

 

 

 

 

 
\t <img alt="picture of smores" height="129" src="images/picture1.jpg" width="194"> 
 
\t <cite> picture taking from <a href="http://www.instructables.com/id/no-campfire-smores/"> http://www.instructables.com/id/no-campfire-smores/ 
 
\t \t \t \t \t \t \t </a></cite> 
 
\t </div> 
 
<footer> 
 
</footer> 
 
</body> 
 
</html>

+0

您有一些错误,https://validator.w3.org/nu/会显示 – Quentin

+0

* alt =“图片的smores”* - 去阅读http://www.alanflavell。 org.uk/alt/alt-text.html – Quentin

+0

就像他们说的,将InnerHTML改为innerHTML。由于这是客户端计算,因此您不需要'form'标记。无论如何你的作品。 – alix

回答

1

您使用InnerHTML(大写我),这是错误的。 右功能.innerHTML


另外,如果你不希望按钮发布形式,就应该将其添加type="button"

更多细节:Can I make a button not submit a form

+0

完美,谢谢。 –

0

你有轻微的错字在你的代码中。您正在使用InnerHTML,但正确的拼写为innerHTML,并带有小写'i'。以下是关于innerHTML的描述:https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML?redirectlocale=en-US&redirectslug=DOM%2Felement.innerHTML。这里是你的固定演示:

<!DOCTYPE html> 
 
<html> 
 
<!--DB 11/2/2015--> 
 
<!-- W3 Schools was referenced in building this code--> 
 
<head> 
 
\t <meta charset="utf-8"> 
 
\t <title>Assignment 8</title> \t 
 
\t <link href="images/avatar.png" rel="shortcut icon" type="image/png"> 
 
\t <link href="css/stylesheet.css" rel="stylesheet" type="text/css"> 
 
\t <script src="js/javascript.js" type="text/javascript"></script> 
 
\t <style type="text/css"> 
 
\t </style> 
 
\t 
 
</head> 
 
<body onload="fillitin()"> 
 
<header> 
 
</header> 
 
<aside> 
 
</aside> 
 
<div id="main"> 
 

 
\t \t 
 
<h1> Assignment 8: Operators and Expression</h1> 
 
<br> 
 
<p id="demo"></p> \t 
 
<script> 
 

 

 
\t var d = new Date(); 
 
\t months = ['Janurary','Feburary','March','April','May','June','July','August','September','October','November','December'] 
 
\t var date = months[d.getMonth()]+" "+d.getDate()+" "+d.getFullYear(); 
 
\t 
 
\t 
 
document.getElementById("demo").innerHTML = date; 
 
</script> 
 
    
 

 

 
<h2 style="text-align:center"> S'mores!</h2> 
 

 
<h4> CLick on the button serves to learn the amount of ingredients needed to make S'mores!</h4> 
 
<table style="width:50%" border="1"> 
 
<form name="myform" method=""> 
 
<tr> 
 
\t <td> <br> 
 
    \t \t <input type="text" id="num1" value="1"> 
 
    \t </td> 
 
\t <td> 
 
\t \t <button onclick="myFunction()">Serves</button> 
 
\t </td> 
 
\t 
 
</tr> 
 
</form> 
 
<tr> 
 
\t <td id="M"></td> 
 
\t <td>Large Marshmallows</td> 
 
</tr> 
 
<tr> 
 
\t <td id="G"></td> 
 
\t <td>Graham Cracker</td> 
 
</tr> 
 
<tr> 
 
\t <td id="C"></td> 
 
\t <td>Ounches Chocolate</td> 
 
</tr> 
 

 
</table> 
 
<script> 
 
\t function fillitin() { 
 
\t document.getElementById("M").innerHTML="1"; 
 
\t document.getElementById("G").innerHTML="1"; 
 
\t document.getElementById("C").innerHTML="1.5"; 
 
\t } 
 
\t 
 
\t function myFunction() { 
 
\t var x=document.getElementById("num1").value; 
 
\t document.getElementById("M").innerHTML=x; 
 
\t document.getElementById("G").innerHTML=x; 
 
\t document.getElementById("C").innerHTML=x*1.5; 
 

 

 
\t } 
 
\t </script> 
 

 
<ul> 
 
<li> 
 
Heat the marshmallow over an open flame until it begins to brown and melt. 
 
</li> 
 
<li>Break the graham cracker in half. Put the chocolate on one half of the cracker, and put the warm marshmallow on the other. </li> 
 
<li>Enjoy! Don’t burn your mouth!</li> 
 
</ul> 
 

 

 

 

 

 
\t <img alt="picture of smores" height="129" src="images/picture1.jpg" width="194"> 
 
\t <cite> picture taking from <a href="http://www.instructables.com/id/no-campfire-smores/"> http://www.instructables.com/id/no-campfire-smores/ 
 
\t \t \t \t \t \t \t </a></cite> 
 
\t </div> 
 
<footer> 
 
</footer> 
 
</body> 
 
</html>

+0

任何想法为什么在浏览器中点击该功能后,它会回到原来的位置? –

0

页面刷新,一旦文件被更改,以便它改变你的价值观,但随后改变了他们回到原来的。您需要找到一个不同的地方来运行fillitin(),然后在文档完成加载时。类似于jquery $(document).ready()