javascript replaceChild 等效于 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18778382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:12:24  来源:igfitidea点击:

replaceChild jQuery equivalent

javascriptjqueryjquery-plugins

提问by Rameez

I'm trying to replace current dom element with new element i made.

我正在尝试用我制作的新元素替换当前的 dom 元素。

I implemented following line

我实现了以下行

$(this).parent().replaceChild(newElem,this);

But it gives error saying $(this).parent().replaceChild is not a functionbecause replaceChild is not a jQuery function, what would be equivalent in jQuery?

但它给出了错误,$(this).parent().replaceChild is not a function因为 replaceChild 不是 jQuery 函数,在 jQuery 中什么是等效的?

回答by David Rz Ayala

If what you want is to replace the inside:

如果你想要的是更换内部:

$(this).empty().append('<div></div>')

回答by sana

If you want to use pure jQuery you can use the replaceWith method

如果你想使用纯 jQuery 你可以使用 replaceWith 方法

$(this).replaceWith(newElem);

you can refer this link :

你可以参考这个链接:

http://api.jquery.com/replaceWith/

http://api.jquery.com/replaceWith/

回答by Sushanth --

You can try this

你可以试试这个

$(this).parent().get().replaceChild(newElem,this);

If $(this).parent()is unique on the page. (.get()gives you the DOM object)

如果$(this).parent()在页面上是唯一的。(.get()给你 DOM 对象)

If you want to use pure jQueryyou can use the replaceWithmethod

如果你想使用纯jQuery你可以使用该replaceWith方法

$(this).replaceWith(newElem);

回答by Rohan Kumar

Try replacewith()

尝试替换()

$(this).parent().replaceWith(newElem);

回答by Gibolt

For anyone looking for a vanilla JS approach, use replaceWith:

对于任何寻找 vanilla JS 方法的人,请使用replaceWith

this.replaceWith(newElem);