|
关键字 |
简介 |
示例代码 |
appendChild
|
追加节点
|
示例代码
|
insertBefore
|
在前方插入节点
|
示例代码
|
|
练习-动态加载js
|
示例代码
|
|
答案-动态加载js
|
示例代码
|
示例
1
:
追加节点
示例
2
:
在前方插入节点
示例
3
:
练习-动态加载js
示例
4
:
答案-动态加载js
通过appendChild追加节点。 追加节点一定是把新的节点插在最后面
1. 创建新节点
2. 获取父节点
3. 通过appendChild追加
");
window.frames["iframe_show961"].document.write(decodeHtml($("textarea#stepcodeTextarea961").val()));
window.frames["iframe_show961"].document.close();
$(window.frames["iframe_show961"]).load(function(){
$("#iframe_show961").height($("#iframe_show961").contents().find("body").height()+showittryitheight);
});
$("#iframe_show961").height($("#iframe_show961").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<div id="parentDiv">
<div id="d1">第一个div</div>
<div id="d2">第二个div</div>
<div id="d3">第三个div</div>
</div>
<script>
function appendDiv(){
var d4= document.createElement("div");
var text = document.createTextNode("第四个div");
d4.appendChild(text);
var parentDiv = document.getElementById("parentDiv");
parentDiv.appendChild(d4);
}
</script>
<button onclick="appendDiv()">追加第4个div</button>
<div id="parentDiv">
<div id="d1">第一个div</div>
<div id="d2">第二个div</div>
<div id="d3">第三个div</div>
</div>
<script>
function appendDiv(){
var d4= document.createElement("div");
var text = document.createTextNode("第四个div");
d4.appendChild(text);
var parentDiv = document.getElementById("parentDiv");
parentDiv.appendChild(d4);
}
</script>
<button onclick="appendDiv()">追加第4个div</button>
");
window.frames["iframe961"].document.write(decodeHtml(code961));
window.frames["iframe961"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe961"]).load(function(){
$("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight);
});
$("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight);
alreadyWriteCode961 = code961;
$("#rendering961").hide();
$("#rendered961").show();
}
var tRereshRetry2DemoPanel961 = setInterval(rereshRetry2DemoPanel961,1000);
var binded961 = false;
$("textarea#stepcodeTextarea961").keyup(function(){
if(!binded961){
$(window).bind('beforeunload',function(){
binded961 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code961 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code961!=newCode){
// window.frames["iframe961"].document.write("
");
// window.frames["iframe961"].document.write(decodeHtml($("textarea#stepcodeTextarea961").val()));
// window.frames["iframe961"].document.close();
// $(window.frames["iframe961"]).load(function(){
// $("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight);
// });
// code961 = newCode;
// }
});
$(".tryButton961").click(function(){
$("#tryDiv961").show();
$("#stepcodeTextarea961").focus();
$("#stepcodeTextarea961").height(200);
$("#iframe961").height(0);
window.frames["iframe961"].document.write("
");
window.frames["iframe961"].document.write(decodeHtml($("textarea#stepcodeTextarea961").val()));
window.frames["iframe961"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe961"]).load(function(){
$("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight);
});
$("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor961.focus();
editor961.setSize(null, "250");
$("#rendering961").hide();
$("#rendered961").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 editor961 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea961"), {
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);
}
}
});
editor961.on("change",function(doc){
if(!binded961){
$(window).bind('beforeunload',function(){
binded961 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code961 = newCode;
$("textarea#stepcodeTextarea961").val(newCode);
if(alreadyWriteCode961!=code961){
lastModifedTime961 = new Date().getTime();
$("#rendering961").show();
$("#rendered961").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor961 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor961.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv961").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
有时候,需要在指定位置插入节点,而不是只是 追加在后面。
这个时候就需要用到 insertBefore
1. 创建新节点
2. 获取父节点
3. 获取需要加入的子节点
4. 通过insertBefore插入
注: insertBefore的第一个参数是新元素,第二个参数是插入位置
");
window.frames["iframe_show962"].document.write(decodeHtml($("textarea#stepcodeTextarea962").val()));
window.frames["iframe_show962"].document.close();
$(window.frames["iframe_show962"]).load(function(){
$("#iframe_show962").height($("#iframe_show962").contents().find("body").height()+showittryitheight);
});
$("#iframe_show962").height($("#iframe_show962").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<div id="parentDiv">
<div id="d1">第一个div</div>
<div id="d2">第二个div</div>
<div id="d3">第三个div</div>
</div>
<script>
function insertDiv(){
var d25= document.createElement("div");
var text = document.createTextNode("第二点五个div");
d25.appendChild(text);
var parentDiv = document.getElementById("parentDiv");
var d3 = document.getElementById("d3");
parentDiv.insertBefore(d25,d3);
}
</script>
<button onclick="insertDiv()">在第二和第三之间,插入第二点五个div</button>
<div id="parentDiv">
<div id="d1">第一个div</div>
<div id="d2">第二个div</div>
<div id="d3">第三个div</div>
</div>
<script>
function insertDiv(){
var d25= document.createElement("div");
var text = document.createTextNode("第二点五个div");
d25.appendChild(text);
var parentDiv = document.getElementById("parentDiv");
var d3 = document.getElementById("d3");
parentDiv.insertBefore(d25,d3);
}
</script>
<button onclick="insertDiv()">在第二和第三之间,插入第二点五个div</button>
");
window.frames["iframe962"].document.write(decodeHtml(code962));
window.frames["iframe962"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe962"]).load(function(){
$("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight);
});
$("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight);
alreadyWriteCode962 = code962;
$("#rendering962").hide();
$("#rendered962").show();
}
var tRereshRetry2DemoPanel962 = setInterval(rereshRetry2DemoPanel962,1000);
var binded962 = false;
$("textarea#stepcodeTextarea962").keyup(function(){
if(!binded962){
$(window).bind('beforeunload',function(){
binded962 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code962 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code962!=newCode){
// window.frames["iframe962"].document.write("
");
// window.frames["iframe962"].document.write(decodeHtml($("textarea#stepcodeTextarea962").val()));
// window.frames["iframe962"].document.close();
// $(window.frames["iframe962"]).load(function(){
// $("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight);
// });
// code962 = newCode;
// }
});
$(".tryButton962").click(function(){
$("#tryDiv962").show();
$("#stepcodeTextarea962").focus();
$("#stepcodeTextarea962").height(200);
$("#iframe962").height(0);
window.frames["iframe962"].document.write("
");
window.frames["iframe962"].document.write(decodeHtml($("textarea#stepcodeTextarea962").val()));
window.frames["iframe962"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe962"]).load(function(){
$("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight);
});
$("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor962.focus();
editor962.setSize(null, "250");
$("#rendering962").hide();
$("#rendered962").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 editor962 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea962"), {
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);
}
}
});
editor962.on("change",function(doc){
if(!binded962){
$(window).bind('beforeunload',function(){
binded962 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code962 = newCode;
$("textarea#stepcodeTextarea962").val(newCode);
if(alreadyWriteCode962!=code962){
lastModifedTime962 = new Date().getTime();
$("#rendering962").show();
$("#rendered962").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor962 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor962.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv962").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
传统的加载js文件的办法是这样 <script src="/study/hello.js"></script>
准备一个test3041.js,其内容是: alert("this is test3041.js");
做一个按钮,当页面打开的时候,并不会加载这个test3041.js,只有在点击了按钮之后,才加载test3041.js
请先登录
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|