操作~DOM替换~replaceAll()replaceWith()

replaceAll()

替换节点

$("<b>Paragraph. </b>").replaceAll("p");

内容.replaceAll(所要替换的节点);

repaceWith()

替换节点并保存原始节点

$('div.second').replaceWith('<h2>New heading</h2>');

所要替换的节点.replaceWith(内容)

replaceWith()可以传入函数方法

obj.replaceWith(function(){

  return $(this).contents();

});

这个方法的意思就是把obj对象中的内容全部复制然后替换掉obj等同与把obj对象删除但是其中的子节点并没有删除

注:如果是JQ选择节点作为替换内容那么原先的JQ节点不是复制过去而是整个搬迁过去

原文地址:https://www.cnblogs.com/BobSky/p/3164905.html