2016-11-16 74 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和使用+兩個值結合起來。