刪除指定位置的節點,如果連續按的話,需要按兩次,有沒有什么改進的地方?
額,在最后,釋放你的洪荒之力,向下滾動頁面吧~
<!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>
2016-04-12
我突然想到了,感謝樓上哥們的“所有的”,我特么可以連著空白節點一塊刪掉啊。。。
2016-04-12
傳入參數為所有的需要刪除的節點就可以了啊