如何使用 jQuery 解开所有父项?
<p><span><a href="link"></a></span></p>
-编辑-
抱歉,我应该提供更多信息。基本上,我试图定位所有被 p
标签包围的 span
的唯一子代 a
。我希望以下内容能够解决问题,但它只解开了 p
标签。我想知道是否有办法删除 span
标签。
$('p > span:only-child > a:only-child').unwrap();
<p><span><a href="link"></a></span></p>
How would I unwrap everything leaving just <a href="link"></a>
?
-edit-
Sorry, I should have provided more information. Basically, I'm trying to target all a
's that are the only child of span
's which are surrounded by p
tags. I was hoping the following would do the trick but it only unwrapped the p
tags. I was wondering if there was a way to remove the span
tags as well.
$('p > span:only-child > a:only-child').unwrap();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做可以做到这一点:
从 DOM 中分离链接并将其放在
link
var 中。从 DOM 中完全删除p
。将a
重新附加到body
标记。要将链接准确地放回到原来的位置,请将其附加到
p
的父元素,而不一定是body
元素。假设您已经得到了这个:您可以使用这一行:
编辑以响应您的编辑:
是的,展开仅执行一个父级别(据我所知)。所以只要做两次就可以了。 :)
This would do it:
Detaches the link from the DOM and sets it aside in the
link
var. Entirely removes thep
from the DOM. Reattaches thea
to thebody
tag.To put the link back precisely where it is, append it to the parent of the
p
, not necessarily thebody
element. So let's say you've got this:You'd use this line instead:
Edit in response to your edit:
Yes, unwrap only does one parent level (as far as I know). So just do it two times. :)
您没有提供有关页面上其他元素的足够信息。以下内容只是为了帮助您入门。
You haven't provided enough information about the other elements on the page. The following is just to get you started.