|
关键字 |
简介 |
示例代码 |
removeChild
|
删除元素节点
|
示例代码
|
removeAttribute
|
删除属性节点
|
示例代码
|
removeChild
|
删除文本节点
|
示例代码
|
示例
1
:
删除元素节点
示例
2
:
删除属性节点
示例
3
:
删除文本节点
要删除某个元素节点有两步
第一:先获取该元素的父节点
第二:通过父节点,调用removeChild 删除该节点
");
window.frames["iframe_show1179"].document.write(decodeHtml($("textarea#stepcodeTextarea1179").val()));
window.frames["iframe_show1179"].document.close();
$(window.frames["iframe_show1179"]).load(function(){
$("#iframe_show1179").height($("#iframe_show1179").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1179").height($("#iframe_show1179").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
function removeDiv(){
var parentDiv = document.getElementById("parentDiv");
var div2= document.getElementById("div2");
parentDiv.removeChild(div2);
}
</script>
<div id="parentDiv">
<div id="div1">安全的div</div>
<div id="div2">即将被删除的div</div>
</div>
<button onclick="removeDiv()">删除第二个div</button>
<script>
function removeDiv(){
var parentDiv = document.getElementById("parentDiv");
var div2= document.getElementById("div2");
parentDiv.removeChild(div2);
}
</script>
<div id="parentDiv">
<div id="div1">安全的div</div>
<div id="div2">即将被删除的div</div>
</div>
<button onclick="removeDiv()">删除第二个div</button>
");
window.frames["iframe1179"].document.write(decodeHtml(code1179));
window.frames["iframe1179"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1179"]).load(function(){
$("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight);
});
$("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight);
alreadyWriteCode1179 = code1179;
$("#rendering1179").hide();
$("#rendered1179").show();
}
var tRereshRetry2DemoPanel1179 = setInterval(rereshRetry2DemoPanel1179,1000);
var binded1179 = false;
$("textarea#stepcodeTextarea1179").keyup(function(){
if(!binded1179){
$(window).bind('beforeunload',function(){
binded1179 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1179 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1179!=newCode){
// window.frames["iframe1179"].document.write("
");
// window.frames["iframe1179"].document.write(decodeHtml($("textarea#stepcodeTextarea1179").val()));
// window.frames["iframe1179"].document.close();
// $(window.frames["iframe1179"]).load(function(){
// $("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight);
// });
// code1179 = newCode;
// }
});
$(".tryButton1179").click(function(){
$("#tryDiv1179").show();
$("#stepcodeTextarea1179").focus();
$("#stepcodeTextarea1179").height(200);
$("#iframe1179").height(0);
window.frames["iframe1179"].document.write("
");
window.frames["iframe1179"].document.write(decodeHtml($("textarea#stepcodeTextarea1179").val()));
window.frames["iframe1179"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1179"]).load(function(){
$("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight);
});
$("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1179.focus();
editor1179.setSize(null, "250");
$("#rendering1179").hide();
$("#rendered1179").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 editor1179 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1179"), {
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);
}
}
});
editor1179.on("change",function(doc){
if(!binded1179){
$(window).bind('beforeunload',function(){
binded1179 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1179 = newCode;
$("textarea#stepcodeTextarea1179").val(newCode);
if(alreadyWriteCode1179!=code1179){
lastModifedTime1179 = new Date().getTime();
$("#rendering1179").show();
$("#rendered1179").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1179 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1179.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1179").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
要删除某个属性节点有两步
第一:先获取该元素节点
第二:元素节点,调用removeAttribute删除指定属性节点
");
window.frames["iframe_show1180"].document.write(decodeHtml($("textarea#stepcodeTextarea1180").val()));
window.frames["iframe_show1180"].document.close();
$(window.frames["iframe_show1180"]).load(function(){
$("#iframe_show1180").height($("#iframe_show1180").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1180").height($("#iframe_show1180").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
function removeHref(){
var link= document.getElementById("link");
link.removeAttribute("href");
}
</script>
<a id="link" href="http://12306.com">http://12306.com</a>
<br>
<button onclick="removeHref()">删除超链的href属性</button>
<script>
function removeHref(){
var link= document.getElementById("link");
link.removeAttribute("href");
}
</script>
<a id="link" href="http://12306.com">http://12306.com</a>
<br>
<button onclick="removeHref()">删除超链的href属性</button>
");
window.frames["iframe1180"].document.write(decodeHtml(code1180));
window.frames["iframe1180"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1180"]).load(function(){
$("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight);
});
$("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight);
alreadyWriteCode1180 = code1180;
$("#rendering1180").hide();
$("#rendered1180").show();
}
var tRereshRetry2DemoPanel1180 = setInterval(rereshRetry2DemoPanel1180,1000);
var binded1180 = false;
$("textarea#stepcodeTextarea1180").keyup(function(){
if(!binded1180){
$(window).bind('beforeunload',function(){
binded1180 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1180 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1180!=newCode){
// window.frames["iframe1180"].document.write("
");
// window.frames["iframe1180"].document.write(decodeHtml($("textarea#stepcodeTextarea1180").val()));
// window.frames["iframe1180"].document.close();
// $(window.frames["iframe1180"]).load(function(){
// $("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight);
// });
// code1180 = newCode;
// }
});
$(".tryButton1180").click(function(){
$("#tryDiv1180").show();
$("#stepcodeTextarea1180").focus();
$("#stepcodeTextarea1180").height(200);
$("#iframe1180").height(0);
window.frames["iframe1180"].document.write("
");
window.frames["iframe1180"].document.write(decodeHtml($("textarea#stepcodeTextarea1180").val()));
window.frames["iframe1180"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1180"]).load(function(){
$("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight);
});
$("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1180.focus();
editor1180.setSize(null, "250");
$("#rendering1180").hide();
$("#rendered1180").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 editor1180 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1180"), {
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);
}
}
});
editor1180.on("change",function(doc){
if(!binded1180){
$(window).bind('beforeunload',function(){
binded1180 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1180 = newCode;
$("textarea#stepcodeTextarea1180").val(newCode);
if(alreadyWriteCode1180!=code1180){
lastModifedTime1180 = new Date().getTime();
$("#rendering1180").show();
$("#rendered1180").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1180 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1180.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1180").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
删除文本节点 1. 通过childNodes[0] 获取文本节点 注:children()[0] 只能获取第一个子元素节点,不能获取文本节点 2. 通过removeChild删除该文本节点 但是这种方式比较麻烦,一般都是直接通过 innerHTML设置为空即可。 注: 通过innerHTML=""的方式,同样会导致文本子节点被删除。
");
window.frames["iframe_show1181"].document.write(decodeHtml($("textarea#stepcodeTextarea1181").val()));
window.frames["iframe_show1181"].document.close();
$(window.frames["iframe_show1181"]).load(function(){
$("#iframe_show1181").height($("#iframe_show1181").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1181").height($("#iframe_show1181").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
function removeDiv1(){
var parentDiv = document.getElementById("parentDiv");
var textNode = parentDiv.childNodes[0];
parentDiv.removeChild(textNode);
}
function removeDiv2(){
var parentDiv = document.getElementById("parentDiv");
parentDiv.innerHTML="";
}
function recover(){
var parentDiv = document.getElementById("parentDiv");
parentDiv.innerHTML="这里是文本 ";
}
</script>
<style>
button{
display:block;
}
</style>
<div id="parentDiv">
这里是文本
</div>
<button onclick="removeDiv1()">通过removechild删除div下的文本节点</button>
<button onclick="removeDiv2()">通过innerHTML让内容置空</button>
<button onclick="recover()">恢复内容</button>
<script>
function removeDiv1(){
var parentDiv = document.getElementById("parentDiv");
var textNode = parentDiv.childNodes[0];
parentDiv.removeChild(textNode);
}
function removeDiv2(){
var parentDiv = document.getElementById("parentDiv");
parentDiv.innerHTML="";
}
function recover(){
var parentDiv = document.getElementById("parentDiv");
parentDiv.innerHTML="这里是文本 ";
}
</script>
<style>
button{
display:block;
}
</style>
<div id="parentDiv">
这里是文本
</div>
<button onclick="removeDiv1()">通过removechild删除div下的文本节点</button>
<button onclick="removeDiv2()">通过innerHTML让内容置空</button>
<button onclick="recover()">恢复内容</button>
");
window.frames["iframe1181"].document.write(decodeHtml(code1181));
window.frames["iframe1181"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1181"]).load(function(){
$("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight);
});
$("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight);
alreadyWriteCode1181 = code1181;
$("#rendering1181").hide();
$("#rendered1181").show();
}
var tRereshRetry2DemoPanel1181 = setInterval(rereshRetry2DemoPanel1181,1000);
var binded1181 = false;
$("textarea#stepcodeTextarea1181").keyup(function(){
if(!binded1181){
$(window).bind('beforeunload',function(){
binded1181 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1181 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1181!=newCode){
// window.frames["iframe1181"].document.write("
");
// window.frames["iframe1181"].document.write(decodeHtml($("textarea#stepcodeTextarea1181").val()));
// window.frames["iframe1181"].document.close();
// $(window.frames["iframe1181"]).load(function(){
// $("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight);
// });
// code1181 = newCode;
// }
});
$(".tryButton1181").click(function(){
$("#tryDiv1181").show();
$("#stepcodeTextarea1181").focus();
$("#stepcodeTextarea1181").height(200);
$("#iframe1181").height(0);
window.frames["iframe1181"].document.write("
");
window.frames["iframe1181"].document.write(decodeHtml($("textarea#stepcodeTextarea1181").val()));
window.frames["iframe1181"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1181"]).load(function(){
$("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight);
});
$("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1181.focus();
editor1181.setSize(null, "250");
$("#rendering1181").hide();
$("#rendered1181").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 editor1181 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1181"), {
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);
}
}
});
editor1181.on("change",function(doc){
if(!binded1181){
$(window).bind('beforeunload',function(){
binded1181 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1181 = newCode;
$("textarea#stepcodeTextarea1181").val(newCode);
if(alreadyWriteCode1181!=code1181){
lastModifedTime1181 = new Date().getTime();
$("#rendering1181").show();
$("#rendered1181").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1181 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1181.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1181").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|