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
replaceChild jQuery equivalent
提问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 function
because 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 :
你可以参考这个链接:
回答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 jQuery
you can use the replaceWith
method
如果你想使用纯jQuery
你可以使用该replaceWith
方法
$(this).replaceWith(newElem);
回答by Rohan Kumar
回答by Gibolt
For anyone looking for a vanilla JS approach, use replaceWith
:
对于任何寻找 vanilla JS 方法的人,请使用replaceWith
:
this.replaceWith(newElem);