jquery 替换

导语 replaceWith()用提供的内容替换集合中所有匹配的元素并且返回被删除元素的集合。 replaceWith()可以从DOM中移除内容,然后在这个地方插入新的内容。通过调用replaceChild处理即可,但是这里需要注意的问题,就是

replaceWith()

用提供的内容替换集合中所有匹配的元素并且返回被删除元素的集合。

.replaceWith()可以从DOM中移除内容,然后在这个地方插入新的内容。

通过调用replaceChild处理即可,但是这里需要注意的问题,就是事件的处理,因为节点的变更所以涉及到要移除这个节点的数据cleanData方法。

replaceWith: function() {
    var arg = arguments[0];
    this.domManip(arguments, function(elem) {
        arg = this.parentNode;
        jQuery.cleanData(getAll(this));
        if (arg) {
            arg.replaceChild(elem, this);
        }
    });
    return arg && (arg.length || arg.nodeType) ? this : this.remove();
}

.replaceWith()方法,和大部分其他jQuery方法一样,返回jQuery对象,所以可以和其他方法链接使用,但是需要注意的是:对于该方法而言,该对象指向已经从 DOM 中被移除的对象,而不是指向替换用的对象。

删除目标节点

jQuery( this ).remove();

然后再插入一个新节点

parent.insertBefore( elem, next );

将匹配元素集合从DOM中删除要涉及到empty,remove,detach方法。

https://www.nucmc.com/ true jquery 替换 https://www.nucmc.com/show-13-601-1.html report 614 replaceWith()用提供的内容替换集合中所有匹配的元素并且返回被删除元素的集合。 replaceWith()可以从DOM中移除内容,然后在这个地方插入新的内容。通过调用replaceChild处理即可,但是这里需要注意的问题,就是
TAG:jquery
本站欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明: 文章转载自:BETWAY官网网 https://www.nucmc.com/show-13-601-1.html
BETWAY官网网 Copyright 2012-2014 www.nucmc.com All rights reserved.(晋ICP备13001436号-1)