replaceWith()和replaceAll()方法都可以用于替换元素或元素中的内容。
但它们调用时,内容和被替换元素所在的位置不同,分别为如下所示:
$(selector).replaceWith(content)
和$(content).replaceAll(selector)
参数selector为被替换的元素,content为替换的内容。
<body>
<h3>使用replaceAll()方法替换元素内容</h3>
<span class="green" title="hi">我是屌丝</span>
<span class="green1" title="hi">我是屌丝</span>
<script type="text/javascript">
var $html = "<span class='red' title='hi'>我是土豪</span>";
$($html).replaceAll(".green");
$(".green1").replaceWith($html);
</script>
</body>
结果是: