C. *_*oss 30 javascript jquery
我正在编写一个脚本,用jQuery中的div替换一些图像.我目前正在使用该replaceWith()
方法,该方法工作正常,但返回原始(已删除)元素而不是新元素.
与大多数jQuery方法一样,.replaceWith()方法返回jQuery对象,以便可以将其他方法链接到它上面.但是,必须注意返回原始 jQuery对象.此对象引用已从DOM中删除的元素,而不是已替换它的新元素.
如何获得刚刚创建的新DOM元素的引用?
qwe*_*ymk 48
$.fn.replaceWithPush = function(a) {
var $a = $(a);
this.replaceWith($a);
return $a;
};
Run Code Online (Sandbox Code Playgroud)
eao*_*son 18
我相信replaceAll()会返回新内容.它的目标和来源与replaceWith()相反.
var newobj = $( "<p>New paragraph" ).replaceAll( "#replaceme" );
Run Code Online (Sandbox Code Playgroud)