how2j.cn

关键字 简介 示例代码
+
-
*
/
%
基本算数运算符
示例代码
++
--
自增,自减运算符
示例代码
=
+=
-=
*=
/=
%=
赋值运算符
示例代码
+
+运算符的多态
示例代码
示例 1 : 基本算数运算符   
示例 2 : 自增,自减运算符   
示例 3 : 赋值运算符   
示例 4 : +运算符的多态   

示例 1 :

基本算数运算符

基本算数运算符和小学中学习的没有任何区别
+ - * / %(取余数)
"); window.frames["iframe_show1099"].document.write(decodeHtml($("textarea#stepcodeTextarea1099").val())); window.frames["iframe_show1099"].document.close(); $(window.frames["iframe_show1099"]).load(function(){ $("#iframe_show1099").height($("#iframe_show1099").contents().find("body").height()+showittryitheight); }); $("#iframe_show1099").height($("#iframe_show1099").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function p(s){ document.write(s); document.write("<br>"); } p("1+2="+ (1+2)); p("5-3="+ (5-3)); p("3*2="+ (3*2)); p("9/3="+ (9/3)); p("10%3="+ (10%3)); </script>
<script>
function p(s){
 document.write(s);
 document.write("<br>");
}

p("1+2="+ (1+2));
p("5-3="+ (5-3));
p("3*2="+ (3*2));
p("9/3="+ (9/3));
p("10%3="+ (10%3));

</script>
"); window.frames["iframe1099"].document.write(decodeHtml(code1099)); window.frames["iframe1099"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1099"]).load(function(){ $("#iframe1099").height($("#iframe1099").contents().find("body").height()+showittryitheight); }); $("#iframe1099").height($("#iframe1099").contents().find("body").height()+showittryitheight); alreadyWriteCode1099 = code1099; $("#rendering1099").hide(); $("#rendered1099").show(); } var tRereshRetry2DemoPanel1099 = setInterval(rereshRetry2DemoPanel1099,1000); var binded1099 = false; $("textarea#stepcodeTextarea1099").keyup(function(){ if(!binded1099){ $(window).bind('beforeunload',function(){ binded1099 = true; return "xxxx"; }); } var newCode = $(this).val() code1099 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1099!=newCode){ // window.frames["iframe1099"].document.write("
"); // window.frames["iframe1099"].document.write(decodeHtml($("textarea#stepcodeTextarea1099").val())); // window.frames["iframe1099"].document.close(); // $(window.frames["iframe1099"]).load(function(){ // $("#iframe1099").height($("#iframe1099").contents().find("body").height()+showittryitheight); // }); // code1099 = newCode; // } }); $(".tryButton1099").click(function(){ $("#tryDiv1099").show(); $("#stepcodeTextarea1099").focus(); $("#stepcodeTextarea1099").height(200); $("#iframe1099").height(0); window.frames["iframe1099"].document.write("
"); window.frames["iframe1099"].document.write(decodeHtml($("textarea#stepcodeTextarea1099").val())); window.frames["iframe1099"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1099"]).load(function(){ $("#iframe1099").height($("#iframe1099").contents().find("body").height()+showittryitheight); }); $("#iframe1099").height($("#iframe1099").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1099.focus(); editor1099.setSize(null, "250"); $("#rendering1099").hide(); $("#rendered1099").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1099 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1099"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1099.on("change",function(doc){ if(!binded1099){ $(window).bind('beforeunload',function(){ binded1099 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1099 = newCode; $("textarea#stepcodeTextarea1099").val(newCode); if(alreadyWriteCode1099!=code1099){ lastModifedTime1099 = new Date().getTime(); $("#rendering1099").show(); $("#rendered1099").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1099 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1099.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1099").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

自增,自减运算符

自增++在原来的基础上 ,增加 1.
需要注意的是
如果 ++放在 前面 ,就是 先运算,后取值
如果 ++放在 后面 ,就是 先取值,后运算
自减 -- 是一个道理。
"); window.frames["iframe_show1101"].document.write(decodeHtml($("textarea#stepcodeTextarea1101").val())); window.frames["iframe_show1101"].document.close(); $(window.frames["iframe_show1101"]).load(function(){ $("#iframe_show1101").height($("#iframe_show1101").contents().find("body").height()+showittryitheight); }); $("#iframe_show1101").height($("#iframe_show1101").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var a = 5; document.write('a++ 是先取值,再运算,所以打印出来是:'+a++); //先取值 ,即5 document.write("<br>"); var b = 5; document.write('++b 是先运算,再取值,所以打印出来是:'+ ++b); //先运算,再取值,即6 </script>
<script>

var a = 5;
document.write('a++ 是先取值,再运算,所以打印出来是:'+a++); //先取值 ,即5

document.write("<br>");
var b = 5;
document.write('++b 是先运算,再取值,所以打印出来是:'+ ++b); //先运算,再取值,即6

</script>
"); window.frames["iframe1101"].document.write(decodeHtml(code1101)); window.frames["iframe1101"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1101"]).load(function(){ $("#iframe1101").height($("#iframe1101").contents().find("body").height()+showittryitheight); }); $("#iframe1101").height($("#iframe1101").contents().find("body").height()+showittryitheight); alreadyWriteCode1101 = code1101; $("#rendering1101").hide(); $("#rendered1101").show(); } var tRereshRetry2DemoPanel1101 = setInterval(rereshRetry2DemoPanel1101,1000); var binded1101 = false; $("textarea#stepcodeTextarea1101").keyup(function(){ if(!binded1101){ $(window).bind('beforeunload',function(){ binded1101 = true; return "xxxx"; }); } var newCode = $(this).val() code1101 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1101!=newCode){ // window.frames["iframe1101"].document.write("
"); // window.frames["iframe1101"].document.write(decodeHtml($("textarea#stepcodeTextarea1101").val())); // window.frames["iframe1101"].document.close(); // $(window.frames["iframe1101"]).load(function(){ // $("#iframe1101").height($("#iframe1101").contents().find("body").height()+showittryitheight); // }); // code1101 = newCode; // } }); $(".tryButton1101").click(function(){ $("#tryDiv1101").show(); $("#stepcodeTextarea1101").focus(); $("#stepcodeTextarea1101").height(200); $("#iframe1101").height(0); window.frames["iframe1101"].document.write("
"); window.frames["iframe1101"].document.write(decodeHtml($("textarea#stepcodeTextarea1101").val())); window.frames["iframe1101"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1101"]).load(function(){ $("#iframe1101").height($("#iframe1101").contents().find("body").height()+showittryitheight); }); $("#iframe1101").height($("#iframe1101").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1101.focus(); editor1101.setSize(null, "250"); $("#rendering1101").hide(); $("#rendered1101").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1101 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1101"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1101.on("change",function(doc){ if(!binded1101){ $(window).bind('beforeunload',function(){ binded1101 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1101 = newCode; $("textarea#stepcodeTextarea1101").val(newCode); if(alreadyWriteCode1101!=code1101){ lastModifedTime1101 = new Date().getTime(); $("#rendering1101").show(); $("#rendered1101").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1101 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1101.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1101").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

赋值运算符

=,+=,-=,*=,/=,%=
x=5 是 最基本的赋值运算符,即把右边的值,赋给左边的变量x。
x+=y, 表示 x=x+y。
其他都是一个道理。
"); window.frames["iframe_show1100"].document.write(decodeHtml($("textarea#stepcodeTextarea1100").val())); window.frames["iframe_show1100"].document.close(); $(window.frames["iframe_show1100"]).load(function(){ $("#iframe_show1100").height($("#iframe_show1100").contents().find("body").height()+showittryitheight); }); $("#iframe_show1100").height($("#iframe_show1100").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> x = 5; x+= 6; document.write('x+=6就是 x=x+6的意思,所以最后的值就等于: '+x); </script>
<script>
   x = 5;
   x+= 6;
   document.write('x+=6就是 x=x+6的意思,所以最后的值就等于: '+x);
</script>
"); window.frames["iframe1100"].document.write(decodeHtml(code1100)); window.frames["iframe1100"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1100"]).load(function(){ $("#iframe1100").height($("#iframe1100").contents().find("body").height()+showittryitheight); }); $("#iframe1100").height($("#iframe1100").contents().find("body").height()+showittryitheight); alreadyWriteCode1100 = code1100; $("#rendering1100").hide(); $("#rendered1100").show(); } var tRereshRetry2DemoPanel1100 = setInterval(rereshRetry2DemoPanel1100,1000); var binded1100 = false; $("textarea#stepcodeTextarea1100").keyup(function(){ if(!binded1100){ $(window).bind('beforeunload',function(){ binded1100 = true; return "xxxx"; }); } var newCode = $(this).val() code1100 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1100!=newCode){ // window.frames["iframe1100"].document.write("
"); // window.frames["iframe1100"].document.write(decodeHtml($("textarea#stepcodeTextarea1100").val())); // window.frames["iframe1100"].document.close(); // $(window.frames["iframe1100"]).load(function(){ // $("#iframe1100").height($("#iframe1100").contents().find("body").height()+showittryitheight); // }); // code1100 = newCode; // } }); $(".tryButton1100").click(function(){ $("#tryDiv1100").show(); $("#stepcodeTextarea1100").focus(); $("#stepcodeTextarea1100").height(200); $("#iframe1100").height(0); window.frames["iframe1100"].document.write("
"); window.frames["iframe1100"].document.write(decodeHtml($("textarea#stepcodeTextarea1100").val())); window.frames["iframe1100"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1100"]).load(function(){ $("#iframe1100").height($("#iframe1100").contents().find("body").height()+showittryitheight); }); $("#iframe1100").height($("#iframe1100").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1100.focus(); editor1100.setSize(null, "250"); $("#rendering1100").hide(); $("#rendered1100").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1100 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1100"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1100.on("change",function(doc){ if(!binded1100){ $(window).bind('beforeunload',function(){ binded1100 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1100 = newCode; $("textarea#stepcodeTextarea1100").val(newCode); if(alreadyWriteCode1100!=code1100){ lastModifedTime1100 = new Date().getTime(); $("#rendering1100").show(); $("#rendered1100").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1100 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1100.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1100").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 4 :

+运算符的多态

+ 具备多态特征
当两边都是数字的时候 ,表现为算数运算符
当任意一边是字符串的时候,表现为字符串连接符
"); window.frames["iframe_show1102"].document.write(decodeHtml($("textarea#stepcodeTextarea1102").val())); window.frames["iframe_show1102"].document.close(); $(window.frames["iframe_show1102"]).load(function(){ $("#iframe_show1102").height($("#iframe_show1102").contents().find("body").height()+showittryitheight); }); $("#iframe_show1102").height($("#iframe_show1102").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write('当+两边都是数字的时候,+就是加法运算符,所以1+2='+(1+2)); document.write("<br>"); document.write('当+任意一边是字符串的时候,+就是字符串连接符,所以1+\"2\"='+ (1+"2")); </script>
<script>
document.write('当+两边都是数字的时候,+就是加法运算符,所以1+2='+(1+2));
document.write("<br>");
document.write('当+任意一边是字符串的时候,+就是字符串连接符,所以1+\"2\"='+ (1+"2"));
</script>
"); window.frames["iframe1102"].document.write(decodeHtml(code1102)); window.frames["iframe1102"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1102"]).load(function(){ $("#iframe1102").height($("#iframe1102").contents().find("body").height()+showittryitheight); }); $("#iframe1102").height($("#iframe1102").contents().find("body").height()+showittryitheight); alreadyWriteCode1102 = code1102; $("#rendering1102").hide(); $("#rendered1102").show(); } var tRereshRetry2DemoPanel1102 = setInterval(rereshRetry2DemoPanel1102,1000); var binded1102 = false; $("textarea#stepcodeTextarea1102").keyup(function(){ if(!binded1102){ $(window).bind('beforeunload',function(){ binded1102 = true; return "xxxx"; }); } var newCode = $(this).val() code1102 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1102!=newCode){ // window.frames["iframe1102"].document.write("
"); // window.frames["iframe1102"].document.write(decodeHtml($("textarea#stepcodeTextarea1102").val())); // window.frames["iframe1102"].document.close(); // $(window.frames["iframe1102"]).load(function(){ // $("#iframe1102").height($("#iframe1102").contents().find("body").height()+showittryitheight); // }); // code1102 = newCode; // } }); $(".tryButton1102").click(function(){ $("#tryDiv1102").show(); $("#stepcodeTextarea1102").focus(); $("#stepcodeTextarea1102").height(200); $("#iframe1102").height(0); window.frames["iframe1102"].document.write("
"); window.frames["iframe1102"].document.write(decodeHtml($("textarea#stepcodeTextarea1102").val())); window.frames["iframe1102"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1102"]).load(function(){ $("#iframe1102").height($("#iframe1102").contents().find("body").height()+showittryitheight); }); $("#iframe1102").height($("#iframe1102").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1102.focus(); editor1102.setSize(null, "250"); $("#rendering1102").hide(); $("#rendered1102").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1102 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1102"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1102.on("change",function(doc){ if(!binded1102){ $(window).bind('beforeunload',function(){ binded1102 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1102 = newCode; $("textarea#stepcodeTextarea1102").val(newCode); if(alreadyWriteCode1102!=code1102){ lastModifedTime1102 = new Date().getTime(); $("#rendering1102").show(); $("#rendered1102").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1102 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1102.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1102").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-算数运算符 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图