how2j.cn


工具版本兼容问题
函数即一段可以重复使用的代码

示例 1 : 函数   
示例 2 : 带参数的函数   
示例 3 : 带返回值的函数   

示例 1 :

函数

function关键字用于定义一个函数
print即函数的名称
()表示参数列表,像这样就指没有参数
{ 表示函数开始
} 表示函数结束
光有函数的定义,还不够,它不会自动执行,还需要进行第5行的调用
"); window.frames["iframe_show1093"].document.write(decodeHtml($("textarea#stepcodeTextarea1093").val())); window.frames["iframe_show1093"].document.close(); $(window.frames["iframe_show1093"]).load(function(){ $("#iframe_show1093").height($("#iframe_show1093").contents().find("body").height()+showittryitheight); }); $("#iframe_show1093").height($("#iframe_show1093").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function print(){ document.write("这一句话是由一个自定义函数打印"); } print(); </script>
<script>
function print(){
  document.write("这一句话是由一个自定义函数打印");
}
print();
</script>
"); window.frames["iframe1093"].document.write(decodeHtml(code1093)); window.frames["iframe1093"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1093"]).load(function(){ $("#iframe1093").height($("#iframe1093").contents().find("body").height()+showittryitheight); }); $("#iframe1093").height($("#iframe1093").contents().find("body").height()+showittryitheight); alreadyWriteCode1093 = code1093; $("#rendering1093").hide(); $("#rendered1093").show(); } var tRereshRetry2DemoPanel1093 = setInterval(rereshRetry2DemoPanel1093,1000); var binded1093 = false; $("textarea#stepcodeTextarea1093").keyup(function(){ if(!binded1093){ $(window).bind('beforeunload',function(){ binded1093 = true; return "xxxx"; }); } var newCode = $(this).val() code1093 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1093!=newCode){ // window.frames["iframe1093"].document.write("
"); // window.frames["iframe1093"].document.write(decodeHtml($("textarea#stepcodeTextarea1093").val())); // window.frames["iframe1093"].document.close(); // $(window.frames["iframe1093"]).load(function(){ // $("#iframe1093").height($("#iframe1093").contents().find("body").height()+showittryitheight); // }); // code1093 = newCode; // } }); $(".tryButton1093").click(function(){ $("#tryDiv1093").show(); $("#stepcodeTextarea1093").focus(); $("#stepcodeTextarea1093").height(200); $("#iframe1093").height(0); window.frames["iframe1093"].document.write("
"); window.frames["iframe1093"].document.write(decodeHtml($("textarea#stepcodeTextarea1093").val())); window.frames["iframe1093"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1093"]).load(function(){ $("#iframe1093").height($("#iframe1093").contents().find("body").height()+showittryitheight); }); $("#iframe1093").height($("#iframe1093").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1093.focus(); editor1093.setSize(null, "250"); $("#rendering1093").hide(); $("#rendered1093").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 editor1093 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1093"), { 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); } } }); editor1093.on("change",function(doc){ if(!binded1093){ $(window).bind('beforeunload',function(){ binded1093 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1093 = newCode; $("textarea#stepcodeTextarea1093").val(newCode); if(alreadyWriteCode1093!=code1093){ lastModifedTime1093 = new Date().getTime(); $("#rendering1093").show(); $("#rendered1093").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1093 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1093.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1093").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

带参数的函数

函数可以接受参数
"); window.frames["iframe_show1094"].document.write(decodeHtml($("textarea#stepcodeTextarea1094").val())); window.frames["iframe_show1094"].document.close(); $(window.frames["iframe_show1094"]).load(function(){ $("#iframe_show1094").height($("#iframe_show1094").contents().find("body").height()+showittryitheight); }); $("#iframe_show1094").height($("#iframe_show1094").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function print(message){ document.write(message); } print("第一句话"); print("<br>"); print("第二句话"); </script>
<script>
function print(message){
  document.write(message);
}
print("第一句话");
print("<br>");
print("第二句话");
</script>
"); window.frames["iframe1094"].document.write(decodeHtml(code1094)); window.frames["iframe1094"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1094"]).load(function(){ $("#iframe1094").height($("#iframe1094").contents().find("body").height()+showittryitheight); }); $("#iframe1094").height($("#iframe1094").contents().find("body").height()+showittryitheight); alreadyWriteCode1094 = code1094; $("#rendering1094").hide(); $("#rendered1094").show(); } var tRereshRetry2DemoPanel1094 = setInterval(rereshRetry2DemoPanel1094,1000); var binded1094 = false; $("textarea#stepcodeTextarea1094").keyup(function(){ if(!binded1094){ $(window).bind('beforeunload',function(){ binded1094 = true; return "xxxx"; }); } var newCode = $(this).val() code1094 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1094!=newCode){ // window.frames["iframe1094"].document.write("
"); // window.frames["iframe1094"].document.write(decodeHtml($("textarea#stepcodeTextarea1094").val())); // window.frames["iframe1094"].document.close(); // $(window.frames["iframe1094"]).load(function(){ // $("#iframe1094").height($("#iframe1094").contents().find("body").height()+showittryitheight); // }); // code1094 = newCode; // } }); $(".tryButton1094").click(function(){ $("#tryDiv1094").show(); $("#stepcodeTextarea1094").focus(); $("#stepcodeTextarea1094").height(200); $("#iframe1094").height(0); window.frames["iframe1094"].document.write("
"); window.frames["iframe1094"].document.write(decodeHtml($("textarea#stepcodeTextarea1094").val())); window.frames["iframe1094"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1094"]).load(function(){ $("#iframe1094").height($("#iframe1094").contents().find("body").height()+showittryitheight); }); $("#iframe1094").height($("#iframe1094").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1094.focus(); editor1094.setSize(null, "250"); $("#rendering1094").hide(); $("#rendered1094").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 editor1094 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1094"), { 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); } } }); editor1094.on("change",function(doc){ if(!binded1094){ $(window).bind('beforeunload',function(){ binded1094 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1094 = newCode; $("textarea#stepcodeTextarea1094").val(newCode); if(alreadyWriteCode1094!=code1094){ lastModifedTime1094 = new Date().getTime(); $("#rendering1094").show(); $("#rendered1094").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1094 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1094.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1094").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

带返回值的函数

自定义一个calc函数,用于计算两个参数的和,并通过return 返回计算结果
"); window.frames["iframe_show1095"].document.write(decodeHtml($("textarea#stepcodeTextarea1095").val())); window.frames["iframe_show1095"].document.close(); $(window.frames["iframe_show1095"]).load(function(){ $("#iframe_show1095").height($("#iframe_show1095").contents().find("body").height()+showittryitheight); }); $("#iframe_show1095").height($("#iframe_show1095").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function print(message){ document.write(message); } function calc(x,y){ return x+y; } var sum = calc(500,20); print(sum); </script>
<script>
function print(message){
  document.write(message);
}

function calc(x,y){
  return x+y;
}

var sum = calc(500,20);
print(sum);

</script>
"); window.frames["iframe1095"].document.write(decodeHtml(code1095)); window.frames["iframe1095"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1095"]).load(function(){ $("#iframe1095").height($("#iframe1095").contents().find("body").height()+showittryitheight); }); $("#iframe1095").height($("#iframe1095").contents().find("body").height()+showittryitheight); alreadyWriteCode1095 = code1095; $("#rendering1095").hide(); $("#rendered1095").show(); } var tRereshRetry2DemoPanel1095 = setInterval(rereshRetry2DemoPanel1095,1000); var binded1095 = false; $("textarea#stepcodeTextarea1095").keyup(function(){ if(!binded1095){ $(window).bind('beforeunload',function(){ binded1095 = true; return "xxxx"; }); } var newCode = $(this).val() code1095 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1095!=newCode){ // window.frames["iframe1095"].document.write("
"); // window.frames["iframe1095"].document.write(decodeHtml($("textarea#stepcodeTextarea1095").val())); // window.frames["iframe1095"].document.close(); // $(window.frames["iframe1095"]).load(function(){ // $("#iframe1095").height($("#iframe1095").contents().find("body").height()+showittryitheight); // }); // code1095 = newCode; // } }); $(".tryButton1095").click(function(){ $("#tryDiv1095").show(); $("#stepcodeTextarea1095").focus(); $("#stepcodeTextarea1095").height(200); $("#iframe1095").height(0); window.frames["iframe1095"].document.write("
"); window.frames["iframe1095"].document.write(decodeHtml($("textarea#stepcodeTextarea1095").val())); window.frames["iframe1095"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1095"]).load(function(){ $("#iframe1095").height($("#iframe1095").contents().find("body").height()+showittryitheight); }); $("#iframe1095").height($("#iframe1095").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1095.focus(); editor1095.setSize(null, "250"); $("#rendering1095").hide(); $("#rendered1095").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 editor1095 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1095"), { 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); } } }); editor1095.on("change",function(doc){ if(!binded1095){ $(window).bind('beforeunload',function(){ binded1095 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1095 = newCode; $("textarea#stepcodeTextarea1095").val(newCode); if(alreadyWriteCode1095!=code1095){ lastModifedTime1095 = new Date().getTime(); $("#rendering1095").show(); $("#rendered1095").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1095 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1095.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1095").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

上传截图