From the below HTML i want to remove the div and H2 tags but keep the UL tag using jquery.
从下面的HTML我想删除div和H2标签,但使用jquery保留UL标签。
Please advise how i can achieve this
请告诉我如何实现这一目标
<div class="row">
<h2><asp:Literal ID="categoryTitle" runat="server"></asp:Literal></h2>
<ul class="tree js-catTree" id="treeContainer" runat="server"></ul>
</div>
Thanks
3 个解决方案
#1
26
You can use replaceWith()
你可以使用replaceWith()
$('.row').replaceWith(function() {
return $('ul', this);
});
#2
14
I stumbled across this page when simply trying to remove a parent element (whilst still keeping the children) - just in case it's useful to others I found unwrap
to be exactly what I needed. For me it was a simple find-an-element-and-unwrap:
当我只是试图删除一个父元素(同时仍然保留孩子们)时,我偶然发现了这个页面 - 以防万一它对其他人有用,我发现解包是我所需要的。对我来说,这是一个简单的find-an-element-and -wwrap:
$('.target-child').unwrap();
However the addition of the h2 removal in this case makes things a little bit more involved:
然而,在这种情况下增加h2去除会使事情变得更加复杂:
$('.row h2').siblings('ul').unwrap().end().remove();
The above should be more optimal as it doesn't rely on the use of an anonymous function creation and call.
以上应该更加优化,因为它不依赖于使用匿名函数创建和调用。
#3
0
You could use jQuery detach()
function ?
你可以使用jQuery detach()函数吗?
This will remove the element (visually) but keep its code in a variable, then you can append it into another parent.
这将删除元素(可视)但将其代码保存在变量中,然后您可以将其附加到另一个父元素中。
#1
26
You can use replaceWith()
你可以使用replaceWith()
$('.row').replaceWith(function() {
return $('ul', this);
});
#2
14
I stumbled across this page when simply trying to remove a parent element (whilst still keeping the children) - just in case it's useful to others I found unwrap
to be exactly what I needed. For me it was a simple find-an-element-and-unwrap:
当我只是试图删除一个父元素(同时仍然保留孩子们)时,我偶然发现了这个页面 - 以防万一它对其他人有用,我发现解包是我所需要的。对我来说,这是一个简单的find-an-element-and -wwrap:
$('.target-child').unwrap();
However the addition of the h2 removal in this case makes things a little bit more involved:
然而,在这种情况下增加h2去除会使事情变得更加复杂:
$('.row h2').siblings('ul').unwrap().end().remove();
The above should be more optimal as it doesn't rely on the use of an anonymous function creation and call.
以上应该更加优化,因为它不依赖于使用匿名函数创建和调用。
#3
0
You could use jQuery detach()
function ?
你可以使用jQuery detach()函数吗?
This will remove the element (visually) but keep its code in a variable, then you can append it into another parent.
这将删除元素(可视)但将其代码保存在变量中,然后您可以将其附加到另一个父元素中。