[ jquery 文档处理 replaceWith(content|fn) replaceAll(content) ] 此方法用于把所有匹配的元素替换成指定的HTML或DOM元素

此方法用于把所有匹配的元素替换成指定的HTML或DOM元素

  content(String, Element, jQuery, Function)

    用于将匹配元素替换掉的内容。如果这里传递一个函数进来的话,函数返回值必须是HTML字符串。

  fn

    返回THML字符串,用来替换的内容。

实例:

<html lang='zh-cn'>
<head>
<title>Insert you title</title>
<meta http-equiv='description' content='this is my page'>
<meta http-equiv='keywords' content='keyword1,keyword2,keyword3'>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type='text/javascript' src='./js/jquery-1.12.1.min.js'></script>
<style type="text/css">
    *{margin:0;padding:0;}
    html{font:400 15px/1.2em 'Courier New';color:#666;750px;margin:25px auto;}
    ul{list-style:none;}
    .hover{color:#FF96EC;}
    .wrapAll{color:#FF96EC;}
</style>
<script type='text/javascript'>
    $(function(){
      // $('.list').replaceWith(document.createElement("div"));
      $('.list').replaceWith($('.wrap'));   // 这种替换方法属于移动替换,不属于复制替换
   // $('.wrap').replaceAll($('.list'));
}); </script> </head> <body> <div id='demo'> <div class='noClassDemo'> <ul class='list'> <li>Index value :</li> <li>Index value :</li> <li>Index value :</li> <li>Index value :</li> <li>Index value :</li> <li>Index value :</li> <li class='active'>Index</li> <li class='wrap'>Index value :</li> <li>Index value :</li> </ul> <div> <p class='list'>this is unwrap test</p> </div> </div> <p class='list'>this is unwrap test</p> </div> </body> </html>
原文地址:https://www.cnblogs.com/mysearchblog/p/5618605.html