課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
設(shè)置兩個輸入框,一個按鈕,實現(xiàn)輸入兩個數(shù),點擊按鈕比較大小怎么寫。
2017-03-16
源自:JavaScript進階篇 5-6
正在回答
我的這個是分別彈出兩個框,還做了是不是整數(shù)的判斷。
function arr(x,y){
? ?var x=prompt('請輸入第一個數(shù)字');
? ? var y=prompt('請輸入第二個數(shù)字');
? ? if(x%1===0 && y%1===0){
? ? ? ? if(x>y){
? ? ? ? ? ?return x;
? ? ? ? }else if(x<y){
? ? ? ? ? ?return y;
? ? ? ? }else{
? ? ? ? ? ? return "兩數(shù)相等"
? ? ? ? }
? ? }else{
? ? ? ? document.write("請輸入整數(shù)");
? ? }
}
//這是我寫的代碼,你可以看看是不是你要的效果
<html>??? <head>??????? <meta http-equiv=content-type content="text/html;charset=GBK">??? </head>??? <body>??????? <input id="txt1" placeholder="輸入第一個值:" />??????? <input id="txt2" placeholder="輸入第二個值:" />??????? <input type="button" value="提交" onclick="sub()">??????? <script>?????????? ???????????? function sub(){??????????????? var content1 = document.getElementById("txt1");??????????????? var content2 = document.getElementById("txt2");??????????????? alert(content1.value);??????????????? alert(content2.value);??????????????? function com(a,b){? ??? ??? ??? ? if(a>b){????? ??? ??? ??? ??? ? document.write("較大的數(shù)是:" +a+"<br>");? ??? ??? ??? ? }else if(a<b){???? ??? ??? ??? ??? ??? document.write("較大的數(shù)是:"+b+"<br>");? ??? ??? ??? ? }else{????? ??? ??? ??? ??? ?? document.write("這兩個數(shù)的值大小相等!");? ??? ??? ??? ? }?? ??? ?}?? ??? ?com(content1.value,content2.value);??????????? }?????????? ???? </script>??? </body>?? ?</html>
首先獲取輸入框中的內(nèi)容,然后通過函數(shù)對兩個數(shù)進行比較。返回大于或小于號。
var?p1=document.getElementById("xxx").value; ... function?compare() { if(p1>p2)?{}; ... }
搖頭的哈士奇 提問者
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
1 回答比較兩個輸入數(shù)字大小,點擊按鈕無反應(yīng)
6 回答我想實現(xiàn),輸入數(shù)字,點擊按鈕就進行轉(zhuǎn)換,同時在另外一個輸入框顯示答案。
3 回答第一次點擊按鈕移除兩個節(jié)點
4 回答能不能用prompt來比較輸入的兩個數(shù)的大?。?/p>
3 回答js比較兩個數(shù)大小
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-03-23
我的這個是分別彈出兩個框,還做了是不是整數(shù)的判斷。
function arr(x,y){
? ?var x=prompt('請輸入第一個數(shù)字');
? ? var y=prompt('請輸入第二個數(shù)字');
? ? if(x%1===0 && y%1===0){
? ? ? ? if(x>y){
? ? ? ? ? ?return x;
? ? ? ? }else if(x<y){
? ? ? ? ? ?return y;
? ? ? ? }else{
? ? ? ? ? ? return "兩數(shù)相等"
? ? ? ? }
? ? }else{
? ? ? ? document.write("請輸入整數(shù)");
? ? }
}
2017-03-17
//這是我寫的代碼,你可以看看是不是你要的效果
<html>
??? <head>
??????? <meta http-equiv=content-type content="text/html;charset=GBK">
??? </head>
??? <body>
??????? <input id="txt1" placeholder="輸入第一個值:" />
??????? <input id="txt2" placeholder="輸入第二個值:" />
??????? <input type="button" value="提交" onclick="sub()">
??????? <script>
?????????? ?
??????????? function sub(){
??????????????? var content1 = document.getElementById("txt1");
??????????????? var content2 = document.getElementById("txt2");
??????????????? alert(content1.value);
??????????????? alert(content2.value);
??????????????? function com(a,b){
? ??? ??? ??? ? if(a>b){
????? ??? ??? ??? ??? ? document.write("較大的數(shù)是:" +a+"<br>");
? ??? ??? ??? ? }else if(a<b){
???? ??? ??? ??? ??? ??? document.write("較大的數(shù)是:"+b+"<br>");
? ??? ??? ??? ? }else{
????? ??? ??? ??? ??? ?? document.write("這兩個數(shù)的值大小相等!");
? ??? ??? ??? ? }
?? ??? ?}
?? ??? ?com(content1.value,content2.value);
??????????? }
?????????? ?
??? </script>
??? </body>
?? ?
</html>
2017-03-16
首先獲取輸入框中的內(nèi)容,然后通過函數(shù)對兩個數(shù)進行比較。返回大于或小于號。