jQuery replace可以工作,但prepend可以';新界?

jQuery replace可以工作,但prepend可以';新界?,jquery,Jquery,错误的一行是: contxt.replaceWith(safe_copy[id]).prepend("<span class='error'>"+jsonObj.data+"</span>"); .replaceWith()返回原始jQuery对象(从页面中删除的对象),因此当链接到该对象时,它将对删除的内容而不是添加的内容进行操作 From for.replaceWith(): .replaceWith()方法与大多数jQuery方法一样,返回 jQuery对象,以便

错误的一行是:

contxt.replaceWith(safe_copy[id]).prepend("<span class='error'>"+jsonObj.data+"</span>");
.replaceWith()
返回原始jQuery对象(从页面中删除的对象),因此当链接到该对象时,它将对删除的内容而不是添加的内容进行操作

From for
.replaceWith()

.replaceWith()方法与大多数jQuery方法一样,返回 jQuery对象,以便其他方法可以链接到该对象上。然而, 必须注意,返回的是原始jQuery对象。这 对象引用已从DOM中删除的元素,而不是 取代它的新元素

我不完全确定我是否理解您的代码/HTML,但这可能是一个可行的解决方法:

var newContent = $(safe_copy[id]).prepend("<span class='error'>"+jsonObj.data+"</span>");
contxt.replaceWith(newContent);
var newContent=$(safe_copy[id]).prepend(“+jsonObj.data+”);
上下文替换为(新内容);

我怀疑以下两个问题:

a. Either the object being returned by replaceWith() was not same as the new node added, or
b. The return object was being regenerated using the original selection criteria
事实证明,您仍然拥有原始对象的句柄,该对象已从DOM中拉出。这在文件中得到了确认

您只需运行选择以再次获取对新节点的引用,然后运行预结束操作。对于所描述的场景,不能将prepend链接到replaceWith()。查看以下显示此方法有效的小提琴示例:


firebug是否输出任何错误?您可以在执行替换之前将新内容预先添加到新内容中,然后替换为已准备好的内容。我不明白你所有的变量都是什么,或者HTML结构足够好,可以给出确切的代码。或者,您可以执行replace()操作,然后再次在页面中查找新内容以进行预处理。
a. Either the object being returned by replaceWith() was not same as the new node added, or
b. The return object was being regenerated using the original selection criteria