我想利用传递参数的方式删除网页元素,代码怎样修改,错哪里?

<!!DOCTYPE html>

<html>

<head>

<title>训练</title>

<meta charset="utf-8" />

<script type="text/javascript">

function jarrem(x){

//var a=document.getElementById("bu");

var child=x.childNodes;

if(child!=null&&child.length>0){

a.removeChild(child[0]);

}

else{

alert("no child");

}

}

</script>

</head>

<body >

<div id="bu" style="border: 1px solid blue;padding: 15px ;align: center;padding: 100px;">

<input type="button" name="button" id="b1" value="1" />

<input type="button" name="button" id="b1" value="2" />

<input type="button" name="button" id="b1" value="3" />

<input type="button" name="button" id="b1" value="4" />

<input type="button" name="button" id="b1" value="5" />

<input type="button" name="button" id="b1" value="6" />

<input type="button" name="button" id="b1" value="7" />

<input type="button" name="button" id="b1" value="8" /><br>


<input type="button" onclick="jarrem(document.getElementById("bu"))"

value="del" style="width :100px; height :50px;"

align="center"/>

</div>

</body>

</html>


Jarrem
浏览 1630回答 2
2回答

qq_青枣工作室_0

我帮你修改了,你能看出差别吗?<!DOCTYPE html> <html> <head> <title>训练</title> <meta charset="utf-8" /> <script type="text/javascript"> function jarrem(x){ //var a=document.getElementById("bu"); var child=x.childNodes; if(child!=null&&child.length>0){ while(child[0]&& child[0].nodeType!=1) x.removeChild(child[0]); x.removeChild(child[0]); } else{ alert("no child"); } } </script> </head> <body > <div id="bu" style="border: 1px solid blue;padding: 15px ;align: center;padding: 100px;"> <input type="button" name="button" id="b1" value="1" /> <input type="button" name="button" id="b1" value="2" /> <input type="button" name="button" id="b1" value="3" /> <input type="button" name="button" id="b1" value="4" /> <input type="button" name="button" id="b1" value="5" /> <input type="button" name="button" id="b1" value="6" /> <input type="button" name="button" id="b1" value="7" /> <input type="button" name="button" id="b1" value="8" /><br> <input type="button" onclick="jarrem(document.getElementById('bu'))" value="del" style="width :100px; height :50px;" align="center"/> </div> </body> </html>

右下角_

function jarrem(x){     //var a=document.getElementById("bu");     var child=x.childNodes;     if(child!=null&&child.length>0){            //a.removeChild(child[0]);//不细心啊            x.removeChild(child[0]);     }     else{          alert("no child");     } }
打开App,查看更多内容
随时随地看视频慕课网APP