1.各个方法详解
(1)replaceWith()
将所有匹配的元素替换成指定的HTML或者DOM元素。
(2)replaceAll()
与replaceWith()功能一样,只是两个对象颠倒了,如同before()和insertBefore()一样。
(3)注意:
替换之前,如果已经为元素绑定事件,替换后原先绑定的事件将与被替换的元素一起消失,需要在元素上重新绑定事件。
2.实例
<html>
<head>
<title>jQuery中的DOM操作.替换节点</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$("a").click(function(){
var $currentFather=$(this).parent();
$currentFather.replaceWith("Hello");
});
});
</script>
</head>
<body>
<p title="Choose your favorite fruit.">What's your favorite fruit?</p>
<ul>
<li>Apple <a href="#">替换为Hello</a></li>
<li>Banana <a href="#">替换为Hello</a></li>
<li>Pineapple <a href="#">替换为Hello</a></li>
</ul>
</body>
</html>