after() 和 remove() 实现替换

<div class="replacedDiv">this is the replaced div</div>

<script>
            $(function(){
                $("div.replacedDiv").after("<p>i will replace the div</p>").remove();
            });
</script>

结果是新建的p替换了原来的div。

 

note: 总是可以将有用的语句封装起来形成jquer拓展

$.fn.replaceWith = function(html){

  return this.after(html).remove();

}

利用这个拓展,执行上面的效果可以用这个语句:

$("div.replaceDiv").replaceWith("<p>i will replace the div</p>");

 

posted @ 2016-02-22 13:38  fangfan  阅读(797)  评论(0编辑  收藏  举报