<html>
<head>
<script type="text/javascript">
function removeChild()
{
var p2 = document.getElementById("p2");
var body = document.getElementById("body");
body.removeChild(p2);
}
</script>
</head>
<body id="body">
<form id="form">
<input type="button" value="btn" onClick="removeChild()">
</form>
<p id="p"><h1>life</h1></p>
<p id="p2">mastery of knowledge</p>
</body>
</html>
这里本来想删除id为p2的这个标签,然后发生了如下的错误:
Uncaught TypeError: Failed to execute 'removeChild' on 'Node': 1 argument required, but only 0 present.(…)
其实这里的错误就是与内置的removeChild()函数重名了。
修改之后的代码:
<html>
<head>
<script type="text/javascript">
function removeChild1()
{
var p2 = document.getElementById("p2");
var body = document.getElementById("body");
body.removeChild(p2);
}
</script>
</head>
<body id="body">
<form id="form">
<input type="button" value="btn" onClick="removeChild1()">
</form>
<p id="p"><h1>life</h1></p>
<p id="p2">mastery of knowledge</p>
</body>
</html>