爱程序网

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>");

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助