replaceNode

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="form1" action="test.html" method="get">
  <input type="text" name="input1" value="input1" id="input1">
  <input type="button" name="input2" value="input2" id="input2">
</form>

<ul>
<li id="a1" value="a1">a1</li>
<li id="a2" value="a2">a2</li>
<li id="a3" value="a3">a3</li>
</ul>
<ul>
<li id="b1" value="b1">b1<p>b11</p></li>
<li id="b2" value="b2">b2</li>
<li id="b3" value="b3">b3</li>
</ul>

<br>
<script type="text/javascript">
    /* document.getElementById("a1").onclick=function(){
    var a1=document.getElementById("a1");
    var b1=document.getElementById("b1");
    var a1P=a1.parentNode;
    var oldElement=a1P.replaceChild(b1,a1);
    //替换之后返回的结果是一个被替换的值
    alert(oldElement.getAttribute("value"));
    }; */
    
    document.getElementById("a1").onclick=function(){
        var a1=document.getElementById("a1");
        var b1=document.getElementById("b1");
        var a1P=this.parentNode;
        var oldElement=a1P.replaceChild(b1,this);
        //替换之后返回的结果是一个被替换的值
        alert(oldElement.getAttribute("value"));
        };
</script>
</body>
</html>

IT文库 » replaceNode
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址