比較兩個輸入數(shù)字大小,點擊按鈕無反應
<!DOCTYPE? HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>比較大小函數(shù)</title>
</head>
<body>
<input type="text" id="num1" name="num1" value="請輸入第一個數(shù)" onfocus="this.value=''" onblur="if(this.value==''){this.value='請輸入第一個數(shù)'}" />
<br />
+<br />
<input type="text" id="num2" name="num2" value="請輸入第二個數(shù)" onfocus="this.value=''" onblur="if(this.value==''){this.value='請輸入第二個數(shù)'}" />
<br />
<input type="button" id="button22" value="其中較大值是" onclick="champ(a,b)" />
<br />
</body>
<script type="text/javascript">
var a=document.getElementById(num1).value;
var b=document.getElementById(num1).value;
function champ(a,b){
??? if(a<b){
??????? return b;
??? }
??? else if(a==b){
??????? return "相等";
??? }
??? else{
??????? return a;
??? }
}
? document.write(champ(a,b)+"<br />");
</script>
</html>
2016-10-13
var a=document.getElementById(num1).value;
var b=document.getElementById(num1).value;
這里, 那兩個id的名字, 要加個引號引起來, 不加的話, 認為是一個變量(且實際上沒有定義). b的值那個id是num2.
num1和num2的值, 你直接寫了, value="請輸入第一個數(shù)";請輸入第二個數(shù), a, b 的值就是這兩句話, 沒有拿到數(shù)字.
可以用placeholder.
a,b的值,放在函數(shù)里自己去獲取的, 那就沒有必要傳參了. 直接自己獲取就可以了.
比較那里不要return, return就跳出函數(shù)了, 也不會打印了.
最后屏幕輸出那里也可以放在函數(shù)里打.
代碼如下:
<!DOCTYPE? HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>比較大小函數(shù)</title>
</head>
<body>
<input type="text" id="num1" name="num1" placeholder="請輸入第一個數(shù)" onfocus="this.value=''" onblur="if(this.value==''){this.value='請輸入第一個數(shù)'}" />
<br />
+<br />
<input type="text" id="num2" name="num2" placeholder="請輸入第二個數(shù)" onfocus="this.value=''" onblur="if(this.value==''){this.value='請輸入第二個數(shù)'}" />
<br />
<input type="button" id="button22" value="其中較大值是" onclick="champ()" />
<br />
</body>
<script type="text/javascript">
function champ(){
??? var a=document.getElementById('num1').value;
??? var b=document.getElementById('num2').value;
??? var res;
??? if(a<b){
??????? res = b;
??? }
??? else if(a==b){
??????? res = "相等";
??? }
??? else{
??????? res = a;
??? }
??? document.write(res);
} ?
</script>
</html>