|
工具版本兼容问题
javascript代码必须放在script标签中
script标签可以放在html的任何地方,一般建议放在head标签里
示例
1
:
script标签
示例
2
:
javascript顺序执行
示例
3
:
使用外部js文件
javascript都是放在script标签中的,一旦加载,就会执行
");
window.frames["iframe_show1067"].document.write(decodeHtml($("textarea#stepcodeTextarea1067").val()));
window.frames["iframe_show1067"].document.close();
$(window.frames["iframe_show1067"]).load(function(){
$("#iframe_show1067").height($("#iframe_show1067").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1067").height($("#iframe_show1067").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<html>
<head>
<script>
document.write("这是 javascript");
</script>
</head>
</html>
<html>
<head>
<script>
document.write("这是 javascript");
</script>
</head>
</html>
");
window.frames["iframe1067"].document.write(decodeHtml(code1067));
window.frames["iframe1067"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1067"]).load(function(){
$("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight);
});
$("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight);
alreadyWriteCode1067 = code1067;
$("#rendering1067").hide();
$("#rendered1067").show();
}
var tRereshRetry2DemoPanel1067 = setInterval(rereshRetry2DemoPanel1067,1000);
var binded1067 = false;
$("textarea#stepcodeTextarea1067").keyup(function(){
if(!binded1067){
$(window).bind('beforeunload',function(){
binded1067 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1067 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1067!=newCode){
// window.frames["iframe1067"].document.write("
");
// window.frames["iframe1067"].document.write(decodeHtml($("textarea#stepcodeTextarea1067").val()));
// window.frames["iframe1067"].document.close();
// $(window.frames["iframe1067"]).load(function(){
// $("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight);
// });
// code1067 = newCode;
// }
});
$(".tryButton1067").click(function(){
$("#tryDiv1067").show();
$("#stepcodeTextarea1067").focus();
$("#stepcodeTextarea1067").height(200);
$("#iframe1067").height(0);
window.frames["iframe1067"].document.write("
");
window.frames["iframe1067"].document.write(decodeHtml($("textarea#stepcodeTextarea1067").val()));
window.frames["iframe1067"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1067"]).load(function(){
$("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight);
});
$("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1067.focus();
editor1067.setSize(null, "250");
$("#rendering1067").hide();
$("#rendered1067").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 editor1067 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1067"), {
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);
}
}
});
editor1067.on("change",function(doc){
if(!binded1067){
$(window).bind('beforeunload',function(){
binded1067 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1067 = newCode;
$("textarea#stepcodeTextarea1067").val(newCode);
if(alreadyWriteCode1067!=code1067){
lastModifedTime1067 = new Date().getTime();
$("#rendering1067").show();
$("#rendered1067").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1067 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1067.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1067").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
示例
2
:
javascript顺序执行
顶
折
如果有多段script代码,会按照从上到下,顺序执行
");
window.frames["iframe_show1068"].document.write(decodeHtml($("textarea#stepcodeTextarea1068").val()));
window.frames["iframe_show1068"].document.close();
$(window.frames["iframe_show1068"]).load(function(){
$("#iframe_show1068").height($("#iframe_show1068").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1068").height($("#iframe_show1068").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
document.write("第一段javascript");
</script>
<script>
document.write("第二段javascript");
</script>
<script>
document.write("第一段javascript");
</script>
<script>
document.write("第二段javascript");
</script>
");
window.frames["iframe1068"].document.write(decodeHtml(code1068));
window.frames["iframe1068"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1068"]).load(function(){
$("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight);
});
$("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight);
alreadyWriteCode1068 = code1068;
$("#rendering1068").hide();
$("#rendered1068").show();
}
var tRereshRetry2DemoPanel1068 = setInterval(rereshRetry2DemoPanel1068,1000);
var binded1068 = false;
$("textarea#stepcodeTextarea1068").keyup(function(){
if(!binded1068){
$(window).bind('beforeunload',function(){
binded1068 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1068 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1068!=newCode){
// window.frames["iframe1068"].document.write("
");
// window.frames["iframe1068"].document.write(decodeHtml($("textarea#stepcodeTextarea1068").val()));
// window.frames["iframe1068"].document.close();
// $(window.frames["iframe1068"]).load(function(){
// $("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight);
// });
// code1068 = newCode;
// }
});
$(".tryButton1068").click(function(){
$("#tryDiv1068").show();
$("#stepcodeTextarea1068").focus();
$("#stepcodeTextarea1068").height(200);
$("#iframe1068").height(0);
window.frames["iframe1068"].document.write("
");
window.frames["iframe1068"].document.write(decodeHtml($("textarea#stepcodeTextarea1068").val()));
window.frames["iframe1068"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1068"]).load(function(){
$("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight);
});
$("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1068.focus();
editor1068.setSize(null, "250");
$("#rendering1068").hide();
$("#rendered1068").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 editor1068 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1068"), {
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);
}
}
});
editor1068.on("change",function(doc){
if(!binded1068){
$(window).bind('beforeunload',function(){
binded1068 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1068 = newCode;
$("textarea#stepcodeTextarea1068").val(newCode);
if(alreadyWriteCode1068!=code1068){
lastModifedTime1068 = new Date().getTime();
$("#rendering1068").show();
$("#rendered1068").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1068 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1068.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1068").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
和css一样,当javascript代码特别多,并且都写在html里的时候,会显得比较繁杂,难以维护。
这个时候可以 采用和css一样的手段,把javascript代码剥离出来,单独放在一个文件里,在html中引用该文件即可。
准备一个 hello.js,里面的是内容是
document.write("hello javascript ");
注意,不要写 script标签
");
window.frames["iframe_show1069"].document.write(decodeHtml($("textarea#stepcodeTextarea1069").val()));
window.frames["iframe_show1069"].document.close();
$(window.frames["iframe_show1069"]).load(function(){
$("#iframe_show1069").height($("#iframe_show1069").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1069").height($("#iframe_show1069").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<html>
<script src="http://127.0.0.1/study/hello.js"></script>
</html>
<html>
<script src="http://127.0.0.1/study/hello.js"></script>
</html>
");
window.frames["iframe1069"].document.write(decodeHtml(code1069));
window.frames["iframe1069"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1069"]).load(function(){
$("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight);
});
$("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight);
alreadyWriteCode1069 = code1069;
$("#rendering1069").hide();
$("#rendered1069").show();
}
var tRereshRetry2DemoPanel1069 = setInterval(rereshRetry2DemoPanel1069,1000);
var binded1069 = false;
$("textarea#stepcodeTextarea1069").keyup(function(){
if(!binded1069){
$(window).bind('beforeunload',function(){
binded1069 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1069 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1069!=newCode){
// window.frames["iframe1069"].document.write("
");
// window.frames["iframe1069"].document.write(decodeHtml($("textarea#stepcodeTextarea1069").val()));
// window.frames["iframe1069"].document.close();
// $(window.frames["iframe1069"]).load(function(){
// $("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight);
// });
// code1069 = newCode;
// }
});
$(".tryButton1069").click(function(){
$("#tryDiv1069").show();
$("#stepcodeTextarea1069").focus();
$("#stepcodeTextarea1069").height(200);
$("#iframe1069").height(0);
window.frames["iframe1069"].document.write("
");
window.frames["iframe1069"].document.write(decodeHtml($("textarea#stepcodeTextarea1069").val()));
window.frames["iframe1069"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1069"]).load(function(){
$("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight);
});
$("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1069.focus();
editor1069.setSize(null, "250");
$("#rendering1069").hide();
$("#rendered1069").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 editor1069 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1069"), {
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);
}
}
});
editor1069.on("change",function(doc){
if(!binded1069){
$(window).bind('beforeunload',function(){
binded1069 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1069 = newCode;
$("textarea#stepcodeTextarea1069").val(newCode);
if(alreadyWriteCode1069!=code1069){
lastModifedTime1069 = new Date().getTime();
$("#rendering1069").show();
$("#rendered1069").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1069 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1069.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1069").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|