jQuery replaceWith找到新元素 [英] jQuery replaceWith find new element

查看:75
本文介绍了jQuery replaceWith找到新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,用jQuery中的div替换一些图像。我目前正在使用 replaceWith() 方法,工作正常,但返回原始(已删除)元素而不是新元素。

I'm writing a script to replace some images with divs in jQuery. I'm currently using the replaceWith() method, which works fine, but returns the original (removed) element instead of the new element.


.replaceWith()方法,如大多数
jQuery方法,返回jQuery
对象,以便其他方法可以
链接到它上面。但是,它必须是
,注意返回原始 jQuery对象
。此对象引用已从
DOM中删除的
元素,而不是已将
替换为它的新元素。

The .replaceWith() method, like most jQuery methods, returns the jQuery object so that other methods can be chained onto it. However, it must be noted that the original jQuery object is returned. This object refers to the element that has been removed from the DOM, not the new element that has replaced it.

如何获得刚刚创建的新DOM元素的引用?

How can I get a reference to the new DOM element I just created?

推荐答案

$.fn.replaceWithPush = function(a) {
    var $a = $(a);

    this.replaceWith($a);
    return $a;
};

这篇关于jQuery replaceWith找到新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆