|
示例
1
:
通过javascript向文档中输出文本
示例
2
:
javascript和DOM结合的一个简单例子
示例
3
:
语言基础,BOM和DOM
示例
1
:
通过javascript向文档中输出文本
顶
折
document是javascript的内置对象,代表浏览器的文档部分 document.write("Hello Javascript"); 向文档写入字符串
");
window.frames["iframe_show1089"].document.write(decodeHtml($("textarea#stepcodeTextarea1089").val()));
window.frames["iframe_show1089"].document.close();
$(window.frames["iframe_show1089"]).load(function(){
$("#iframe_show1089").height($("#iframe_show1089").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1089").height($("#iframe_show1089").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
document.write("Hello Javascript");
</script>
<script>
document.write("Hello Javascript");
</script>
");
window.frames["iframe1089"].document.write(decodeHtml(code1089));
window.frames["iframe1089"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1089"]).load(function(){
$("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight);
});
$("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight);
alreadyWriteCode1089 = code1089;
$("#rendering1089").hide();
$("#rendered1089").show();
}
var tRereshRetry2DemoPanel1089 = setInterval(rereshRetry2DemoPanel1089,1000);
var binded1089 = false;
$("textarea#stepcodeTextarea1089").keyup(function(){
if(!binded1089){
$(window).bind('beforeunload',function(){
binded1089 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1089 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1089!=newCode){
// window.frames["iframe1089"].document.write("
");
// window.frames["iframe1089"].document.write(decodeHtml($("textarea#stepcodeTextarea1089").val()));
// window.frames["iframe1089"].document.close();
// $(window.frames["iframe1089"]).load(function(){
// $("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight);
// });
// code1089 = newCode;
// }
});
$(".tryButton1089").click(function(){
$("#tryDiv1089").show();
$("#stepcodeTextarea1089").focus();
$("#stepcodeTextarea1089").height(200);
$("#iframe1089").height(0);
window.frames["iframe1089"].document.write("
");
window.frames["iframe1089"].document.write(decodeHtml($("textarea#stepcodeTextarea1089").val()));
window.frames["iframe1089"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1089"]).load(function(){
$("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight);
});
$("#iframe1089").height($("#iframe1089").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1089.focus();
editor1089.setSize(null, "250");
$("#rendering1089").hide();
$("#rendered1089").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 editor1089 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1089"), {
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);
}
}
});
editor1089.on("change",function(doc){
if(!binded1089){
$(window).bind('beforeunload',function(){
binded1089 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1089 = newCode;
$("textarea#stepcodeTextarea1089").val(newCode);
if(alreadyWriteCode1089!=code1089){
lastModifedTime1089 = new Date().getTime();
$("#rendering1089").show();
$("#rendered1089").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1089 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1089.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1089").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
示例
2
:
javascript和DOM结合的一个简单例子
顶
折
这是一个javascript和DOM结合的一个简单例子 onclick="..." 表示点击button之后做的事情 document.getElementById 根据id获取指定的元素 .style.display='none' 隐藏 .style.display='block' 显示(准确的讲,以block的形式显示) onclick,getElementById,style.display 这些内容,是 HTML DOM 应该到才会用到的知识和概念
");
window.frames["iframe_show2094"].document.write(decodeHtml($("textarea#stepcodeTextarea2094").val()));
window.frames["iframe_show2094"].document.close();
$(window.frames["iframe_show2094"]).load(function(){
$("#iframe_show2094").height($("#iframe_show2094").contents().find("body").height()+showittryitheight);
});
$("#iframe_show2094").height($("#iframe_show2094").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<button onclick="document.getElementById('text').style.display='none'">隐藏文本</button>
<button onclick="document.getElementById('text').style.display='block'">显示文本</button>
<p id="text"> 这个是一段可以被javascript隐藏的文本</p>
<button onclick="document.getElementById('text').style.display='none'">隐藏文本</button>
<button onclick="document.getElementById('text').style.display='block'">显示文本</button>
<p id="text"> 这个是一段可以被javascript隐藏的文本</p>
");
window.frames["iframe2094"].document.write(decodeHtml(code2094));
window.frames["iframe2094"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe2094"]).load(function(){
$("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight);
});
$("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight);
alreadyWriteCode2094 = code2094;
$("#rendering2094").hide();
$("#rendered2094").show();
}
var tRereshRetry2DemoPanel2094 = setInterval(rereshRetry2DemoPanel2094,1000);
var binded2094 = false;
$("textarea#stepcodeTextarea2094").keyup(function(){
if(!binded2094){
$(window).bind('beforeunload',function(){
binded2094 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code2094 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code2094!=newCode){
// window.frames["iframe2094"].document.write("
");
// window.frames["iframe2094"].document.write(decodeHtml($("textarea#stepcodeTextarea2094").val()));
// window.frames["iframe2094"].document.close();
// $(window.frames["iframe2094"]).load(function(){
// $("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight);
// });
// code2094 = newCode;
// }
});
$(".tryButton2094").click(function(){
$("#tryDiv2094").show();
$("#stepcodeTextarea2094").focus();
$("#stepcodeTextarea2094").height(200);
$("#iframe2094").height(0);
window.frames["iframe2094"].document.write("
");
window.frames["iframe2094"].document.write(decodeHtml($("textarea#stepcodeTextarea2094").val()));
window.frames["iframe2094"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe2094"]).load(function(){
$("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight);
});
$("#iframe2094").height($("#iframe2094").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor2094.focus();
editor2094.setSize(null, "250");
$("#rendering2094").hide();
$("#rendered2094").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 editor2094 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea2094"), {
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);
}
}
});
editor2094.on("change",function(doc){
if(!binded2094){
$(window).bind('beforeunload',function(){
binded2094 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code2094 = newCode;
$("textarea#stepcodeTextarea2094").val(newCode);
if(alreadyWriteCode2094!=code2094){
lastModifedTime2094 = new Date().getTime();
$("#rendering2094").show();
$("#rendered2094").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor2094 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor2094.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv2094").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
完整的javascript由 语言基础, BOM和 DOM组成。 只有通过javascript操作DOM对象的时候,才会带来很好的实用效果,而本章节只会涉及到语言基础 和 BOM部分,DOM部分在 下个章节展开
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|