2016-11-16 81 views
-7

在程序中,我试图接受来自用户的no,并在标签上显示no是正数或负数。
如何分配标签?Html在标签上使用javascript显示输出

<html> 
 
\t <Head> 
 
\t \t <title> \t If Else Demo Program</title> 
 
\t \t 
 
<style> 
 
thead {color:green;} 
 
tbody {color:green;} 
 
tfoot {color:red;} 
 

 
table{ 
 
    \t border: 1px solid black; 
 
\t border-color:red; 
 
} 
 
#No1{ 
 
\t background-color:lightyellow; 
 
} 
 
tr{ 
 
\t border:1px solid black; 
 
\t border-color:pink; 
 
} 
 
td{ 
 
\t border:1px solid black; 
 
\t border-color:gray; 
 
} 
 
</style> 
 

 
</Head> 
 
\t \t <body> 
 
\t \t <table> 
 
\t \t <thead> 
 
\t \t <tr> 
 
\t \t \t <td>Enter a no</td> 
 
\t \t \t <td><input type="text" name="No1" id="No1"><br></td> 
 
\t \t </tr> 
 
\t \t </thead> 
 
\t \t <tbody> 
 
\t \t <tr> 
 
\t \t \t <td> 
 
\t \t \t \t <h3>Body section with green color</h3> 
 
\t \t \t </td> 
 
\t \t \t <td><label for="Output" name="lblMessage" id="Message">output</label> </td> 
 
\t \t </tr> 
 
\t \t </tbody> 
 
\t \t <tr> 
 
\t \t <tbody> 
 
\t \t \t <td> 
 
\t \t \t \t <input type="Submit" onclick="Demo()"> 
 
\t \t \t </td> 
 
\t \t </tbody> 
 
\t \t </tr> 
 
\t \t 
 
\t \t </table> 
 
\t \t 
 
\t \t <script type="Text/JavaScript" lang="JavaScript"> 
 
\t \t function Demo() 
 
\t \t { 
 
\t \t \t var No1=parseInt(document.getElementById("No1").value); 
 
\t \t 
 
\t \t \t if(No1>0) 
 
\t \t \t \t document.getElementById("Message").InnerHTML=("Positve No ",No1); 
 
\t \t \t else 
 
\t \t \t \t document.getElementById("Message").InnerHTML=("Negative No ",No1); 
 
\t \t 
 
\t \t } 
 
\t \t </script> 
 
\t \t 
 
\t \t </body> 
 
</html>

+2

没有'InnerHTML'属性。你的意思是'innerHTML'。 '(“Positve No”,No1)'由于逗号运算符的原因将仅评估为'No1'。试试'+'。并且'type =“Text/JavaScript”'应该是'type =“text/javascript”'。 – Xufox

+0

欢迎来到StackOverflow!在发布任何问题之前请遵循这些指南! http://stackoverflow.com/help/how-to-ask –

回答

1

你有一个小语法错误,innerHTML的实际上是应该的innerHTML。也是一个重构推荐。

function Demo() { 

     var No1 = parseInt(document.getElementById("No1").value); 
     var Message = document.getElementById("Message"); 

     if (No1 > 0) { 
      Message.innerHTML = "Positive number " + No1; 
     } else { 
      Message.innerHTML = "Negative number " + No1; 
     } 
    } 
0
if(num1>0) 
    document.getElementById("message").innerHTML=("Positve No " + num1); 
else 
    document.getElementById("message").innerHTML=("Negative No "+ num1); 

InnerHTML应该innerHTML和使用+两个值结合起来。