删除指定位置的节点,如果连续按的话,需要按两次,有没有什么改进的地方?
额,在最后,释放你的洪荒之力,向下滚动页面吧~
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<div id="content">
<h1>html</h1>
<h1>php</h1>
<h1>javascript</h1>
<h1>jquery</h1>
<h1>java</h1>
</div>
<script type="text/javascript">
//First Method
/*function clearText1() {
var content=document.getElementById("content");
content.removeChild(content.childNodes[5]);*/ // 在此完成该函数
//Second Method
/*function clearText2(){
var content=document.getElementById("content");
var A=content.childNodes[5];
content.removeChild(A);*/
//Compelet Remove Object
/*function compeletRemoveText(){
var content=document.getElementById("content");
var A=content.childNodes[5];
A=null;
content.removeChild(A);*/
//Remove All Object
/*function RemoveAllText(){
var content=document.getElementById("content");
var nodel=content.childNodes.length
for(i=0;i<nodel;i++){
content.removeChild(content.childNodes[0]);
}*/
//Remove Selected Object
function RemoveSelectedText(){
var content=document.getElementById("content");
var i=document.getElementById("select").value-1;
switch (i) //或可嵌套函数将i值由[1,2,3,4,5]变为[1,3,5,7,9],对应每一元素
{case 0:
content.removeChild(content.childNodes[1]);break;
case 1:
content.removeChild(content.childNodes[3]);break;
case 2:
content.removeChild(content.childNodes[5]);break;
case 3:
content.removeChild(content.childNodes[7]);break;
case 4:
content.removeChild(content.childNodes[9]);break;
default:
alert("你不要输入这种莫名其妙的东西好吗?!");} //只能用一次,连续按的话,得多按一次,因为有空白节点,我这个恨啊(╯°Д°)╯︵ ┻━┻
/*if(content.childNodes[i]!=1){ //这个方法不太科学,如非IE得计算空白节点
content.removeChild(content.childNodes[i].nextSibling.nextSibling);
}
else{
content.removeChild(content.childNodes[i]);
}*/
}
</script>
<input type="text" id="select">
<button onclick="RemoveSelectedText()">清除节点内容</button>
</body>
</html>