|
示例
1
:
函数
示例
2
:
带参数的函数
示例
3
:
带返回值的函数
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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
");
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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
自定义一个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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|