Javascript déve...LOGIN

Javascript développe les effets d'addition et de soustraction de l'addition et de la soustraction du panier

Développement d'effets d'addition et de soustraction

partie HTML du code :

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>购物车加减</title>
 <style type="text/css">
 a{
 text-decoration: none;display:block;width:30px;height:30px;
 background:#eee;line-height:30px;font-weight:bolder;
 }
 .body{width:500px;height:300px;background:#ccc;margin:0 auto;text-align:center;padding:80px;}
 #a1{float:left;margin-right:20px;margin-top:2px;text-align:center;}
 form{float:left;}
 form input{width:40px;height:30px;border:1px solid #eee;text-align:center;}
 #a2{float:left;margin-left:20px;margin-top:2px;text-align:center;}
 </style> 
</head>
<body>
 <div>
 <a href="#" id="a1">-</a>
 <form>
 <input type= "text" value="1" id='id'>
 </form>
 <a href="#" id="a2">+</a>
 </div>
</body>
</html>

Nous devons d'abord obtenir la valeur de l'entrée dans le formulaire, qui est 1

var input = document.getElementById ('id').value;

est stocké dans une variable

Lorsque l'on clique sur le signe moins, le code est le suivant : = function(){

                                                                                                                                                                                                                                                                               getElementById('id').value>1){

                                                                                                                                                                             .         }else{
                                      document.getElementById('id').                                                                                                                                                 

document.getElementById('a2').onclick = function(){
if(isNaN(document.getElementById('id').value)){
alert("Veuillez entrer un numéro")
                                                                                                                ;                                                                                   v1=parseInt(v1);
                                                                                                                                                                                 via v1=parseInt(v1);
                       via document.getElementById('id').value = v1 + 1 ;
                                                                                                                                      lur = function(){
          var id = document.getElementById('id').value; ");
document.getElementById ('id').value = 1;

                                                                                    return false ;                   
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>购物车加减</title>
 <style type="text/css">
 a{
 text-decoration: none;display:block;width:30px;height:30px;
 background:#eee;line-height:30px;font-weight:bolder;
 }
 .body{width:500px;height:300px;background:#ccc;margin:0 auto;text-align:center;padding:80px;}
 #a1{float:left;margin-right:20px;margin-top:2px;text-align:center;}
 form{float:left;}
 form input{width:40px;height:30px;border:1px solid #eee;text-align:center;}
 #a2{float:left;margin-left:20px;margin-top:2px;text-align:center;}
 </style>
 <script type="text/javascript">
            window.onload=function(){
                var input = document.getElementById('id').value;
                document.getElementById('a1').onclick = function(){
                        if(isNaN(document.getElementById('id').value)){
                            alert("请输入数字");
                        }else{                                
                            if(document.getElementById('id').value>1){
                                document.getElementById('id').value = parseInt(document.getElementById('id').value) - 1;
                            }else{
                                document.getElementById('id').value = 1;
                            }
                        }
                        
                    }
                document.getElementById('a2').onclick = function(){

                        if(isNaN(document.getElementById('id').value)){
                            alert("请输入数字");
                        }else{                            
                            var v1 = document.getElementById('id').value;
                            v1=parseInt(v1);
                            document.getElementById('id').value = v1 + 1;
                        }
                    }
                document.getElementById('id').onblur = function(){
                    var id = document.getElementById('id').value;
                    if(isNaN(id) || id < 1){
                        alert("请输入数字");
                        document.getElementById('id').value = 1;
                        return false;
                    }
                }
                
            }
    </script>
</head>
<body>
 <div>
 <a href="#" id="a1">-</a>
 <form>
 <input type= "text" value="1" id='id'>
 </form>
 <a href="#" id="a2">+</a>
 </div>
</body>
</html>

De cette façon, nous pouvons réaliser du shopping L'effet de l'ajout et de la soustraction de numéros de voiture

section suivante

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>购物车加减</title> <style type="text/css"> a{ text-decoration: none;display:block;width:30px;height:30px; background:#eee;line-height:30px;font-weight:bolder; } .body{width:500px;height:300px;background:#ccc;margin:0 auto;text-align:center;padding:80px;} #a1{float:left;margin-right:20px;margin-top:2px;text-align:center;} form{float:left;} form input{width:40px;height:30px;border:1px solid #eee;text-align:center;} #a2{float:left;margin-left:20px;margin-top:2px;text-align:center;} </style> <script type="text/javascript"> window.onload=function(){ var input = document.getElementById('id').value; document.getElementById('a1').onclick = function(){ if(isNaN(document.getElementById('id').value)){ alert("请输入数字"); }else{ if(document.getElementById('id').value>1){ document.getElementById('id').value = parseInt(document.getElementById('id').value) - 1; }else{ document.getElementById('id').value = 1; } } } document.getElementById('a2').onclick = function(){ if(isNaN(document.getElementById('id').value)){ alert("请输入数字"); }else{ var v1 = document.getElementById('id').value; v1=parseInt(v1); document.getElementById('id').value = v1 + 1; } } document.getElementById('id').onblur = function(){ var id = document.getElementById('id').value; if(isNaN(id) || id < 1){ alert("请输入数字"); document.getElementById('id').value = 1; return false; } } } </script> </head> <body> <div> <a href="#" id="a1">-</a> <form> <input type= "text" value="1" id='id'> </form> <a href="#" id="a2">+</a> </div> </body> </html>
soumettreRéinitialiser le code
chapitredidacticiel