Exemple simple d'addition, de soustraction, de multiplication et de division en javascript
<html> <éad- head> <meta http-equiv = "contenu-type" contenu = "text / html; charset = utf-8" /> </ head> <script linguisse = "javascript" type = "text / javascript"> // divide function accdiv (arg1, arg2) {var t1 = 0, t2 = 0, r1, r2, n; essayez {t1 = arg1.toString (). Split (".") [1] .length; } catch (e) {t1 = 0;} try {t2 = arg2.ToString (). Split (".") [1] .length; } catch (e) {t2 = 0;} avec (math) {r1 = nombre (arg1.toString (). Remplace (".", ")); r2 = nombre (arg2.ToString (). Remplace (". ",")); n = math.max (t1, t2); return (r1 / r2) * pow (10, t2-t1); }} // Fonction multiple accmul (arg1, arg2) {var t1 = 0, t2 = 0, r1, r2; essayez {t1 = arg1.toString (). Split (".") [1] .length; } catch (e) {t1 = 0;} try {t2 = arg2.ToString (). Split (".") [1] .length; } catch (e) {t2 = 0;} avec (math) {r1 = nombre (arg1.toString (). Remplace (".", ")); r2 = nombre (arg2.ToString (). Remplace (". ",")); return (r1 * r2) / pow (10, t2 + t1); }} // Ajouter une fonction Accadd (arg1, arg2) {var t1 = 0, t2 = 0, m; essayez {t1 = arg1.toString (). Split (".") [1] .length; } catch (e) {t1 = 0;} try {t2 = arg2.ToString (). Split (".") [1] .length; } catch (e) {t2 = 0;} avec (math) {m = math.pow (10, math.max (t1, t2)); return (arg1 * m + arg2 * m) / m; }} // fonction de soustraction AccSubtr (arg1, arg2) {var t1 = 0, t2 = 0, m, n; essayez {t1 = arg1.toString (). Split (".") [1] .length; } catch (e) {t1 = 0;} try {t2 = arg2.ToString (). Split (".") [1] .length; } catch (e) {t2 = 0;} avec (math) {// la longueur de précision de contrôle dynamique n = math.max (t1, t2); m = math.pow (10, n); // return (arg1 * m - arg2 * m) / m; return ((arg1 * m - arg2 * m) / m) .tofixed (n); }} // Ajoutez une méthode div au type de chaîne, qui est plus pratique à appeler. String.prototype.div = fonction (arg) {return accdiv (this, arg);} // ajouter une méthode MUL au type de chaîne, ce qui le rend plus pratique pour appeler. String.prototype.mul = fonction (arg) {return accmul (arg, this);} // ajouter une méthode ajouter au type de chaîne, ce qui le rend plus pratique pour appeler. String.prototype.add = fonction (arg) {return accadd (arg, this);} // ajouter une méthode subtr au type de chaîne, ce qui le rend plus pratique pour appeler. String.prototype.subtr = function (arg) {return acsubtr (this, arg);} function cal () {var arg1 = document.form1.textbox1.value; var arg2 = document.form1.textbox2.value; //Document.Form1.TextBox5.Value = ACCDIV (Arg1, Arg2); //Document.Form1.TextBox6.Value = AccMul (Arg1, Arg2); //Document.Form1.TextBox7.Value = Accadd (Arg1, Arg2); //Document.Form1.TextBox8.Value = ACCSUBTR (Arg1, Arg2); document.form1.textbox5.value = arg1.div (arg2); document.form1.textbox6.value = arg1.mul (arg2); document.form1.textbox7.value = arg1.add (arg2); document.form1.textbox8.value = arg1.subtr (arg2);} </script> <body> <form1 = "form1" name = "form1" metheth = "post" runat = "server"> <div style = "border: solide 1px # 000000; width: 600px;"> <div style = "float: Left; width: 30%;" Value = "0" name = "TextBox1" /> </ div> <div style = "float: Left; width: 30%;"> <input id = "textbox2" value = "0" type = "text" name = "textbox2" /> </ div> <div style = "float: left; width: 30%;"> <div> accdiv: <put id = "textbox5" Type = "Text =" text " <v> accmul: <input id = "textbox6" type = "text" name = "textbox6" /> </ div> <div> accadd: <input id = "textbox7" type = "text" name = "textbox7" /> </ div> <v> accuSubtr: <input id = "TextBox" type = "Text" name = "textbox8" /> </ div> Largeur: 10%; "> <input type =" Button "name =" aa "value =" cal "onclick =" cal (); " /> </div> </ div> </ form> </ body> </html>L'exemple simple ci-dessus d'addition, de soustraction, de multiplication et de division de JavaScript est tout le contenu que je partage avec vous. J'espère que vous pourrez vous faire référence et j'espère que vous pourrez soutenir Wulin.com plus.