Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript replaceChild jQuery等价物_Javascript_Jquery_Jquery Plugins - Fatal编程技术网

Javascript replaceChild jQuery等价物

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(

我正在尝试用我创建的新元素替换当前的dom元素

我执行了以下命令

$(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
它将替换父元素。