Javascript replaceChild jQuery等价物
我正在尝试用我创建的新元素替换当前的dom元素 我执行了以下命令Javascript replaceChild jQuery等价物,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我正在尝试用我创建的新元素替换当前的dom元素 我执行了以下命令 $(this).parent().replaceChild(newElem,this); 但是它给出了一个错误,说:$(this).parent().replaceChild不是一个函数,因为replaceChild不是一个jQuery函数,那么在jQuery中什么是等价的呢?您可以试试这个 $(this).parent().get().replaceChild(newElem,this); 如果$(this).parent(
$(this).parent().replaceChild(newElem,this);
但是它给出了一个错误,说:$(this).parent().replaceChild不是一个函数
,因为replaceChild不是一个jQuery函数,那么在jQuery中什么是等价的呢?您可以试试这个
$(this).parent().get().replaceChild(newElem,this);
如果$(this).parent()
在页面上是唯一的。(.get()
提供DOM对象)
如果要使用纯jQuery
,可以使用replaceWith
方法
$(this).replaceWith(newElem);
试一试
如果要使用纯jQuery,可以使用replaceWith方法
$(this).replaceWith(newElem);
您可以参考此链接:
如果要更换内部:
$(this).empty().append('<div></div>')
$(this.empty().append(“”)
对于寻求普通JS方法的任何人,请使用replaceWith
:
this.replaceWith(newElem);
如果要替换父级中的元素,请执行以下操作 这是如何替换元素的所有子元素:
$(this).children().replaceWith(<div>hello world</div>)
$($(this).children()[3]).replaceWith(<div>hello world</div>)
$(this.children().replaceWith(hello world)
这是如何用索引替换元素的特定子元素:
$(this).children().replaceWith(<div>hello world</div>)
$($(this).children()[3]).replaceWith(<div>hello world</div>)
$($(this.children()[3]).replaceWith(hello world)
它替换父元素。如果您只想替换当前的元素。。删除.parent
它将替换父元素。