輸入框比較大小彈出較大值
<!DOCTYPE ?HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函數(shù)</title>
<script type="text/javascript">
var a=prompt("輸入數(shù)字");
var b=prompt("輸入另一個數(shù)字");
function compare(a,b)
? ? { ?
? ? ? ? if(a>b)
? ? ? ? {
? ? ? ? ? ?return a;
? ? ? ? }
? ? ? ? else if(a<b)
? ? ? ? {
? ? ? ? ? ?return b;
? ? ? ? }else
? ? ? ? {return "兩數(shù)相等"}
? ? } ? ?
? document.write(a+"和"+b+"的較大值是:"+compare(a,b));
</script>
</head>
<body>
</body>
</html>
2018-08-14
輸入框輸入的時候? 用 parseInt? :取整數(shù) ,忽略小數(shù),到達(dá)第一個非數(shù)字類型截止, 數(shù)字類型必須在非數(shù)字類型的前面,否者無效
寫法:
var a=parseInt? (prompt("輸入數(shù)字"));
var b=parseInt? (prompt("輸入另一個數(shù)字"));
2018-08-02
知識有限,之前老師提過if else 中不要使用return會跳出
function compare(a,b)
? ? {?
? ? ? ? if(a>=b)
? ? ? ? {
? ? ? ? ?? bigger=a;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ?? bigger=b;
? ? ? ? }
return bigger;
}
望指正
2018-08-02
你是想在輸入框獲取比較的值,并且在輸入框輸出嗎?我知識有限就只能這樣了。下面是我的代碼
<!DOCTYPE? HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函數(shù)</title>
<script type="text/javascript">
var a=parseInt(document.getElementById("input1").value);
var b=parseInt(document.getElementById("input2").value);
function compare(a,b)
? ? {??
? ? var t=document.getElementById("output");
? ? ? ? if(a>b)
? ? ? ? {
? ? ? ? ? ?t.value= a;
? ? ? ? }
? ? ? ? else if(a<b)
? ? ? ? {
? ? ? ? ? t.value= b;
? ? ? ? }else
? ? ? ? { t.value= "兩數(shù)相等"}
? ? }
? ??
</script>
</head>
<body>
? ? ? ?<input type="text" id="input1" placeHolder="在這里輸入第一個數(shù)字">
? ? ? ?<input type="text" id="input2" placeHolder="在這里輸入第二個數(shù)字">
? ? ? ?<input type="text" id="output" placeHolder="這里是比較結(jié)果">
? ? ? ?<input type="button" onclick="compare()" value="compare">
</body>
</html>
2018-08-02
有什么問題嗎?