[英]How do I use .remove on this <p> that has an <a> with a specific class?
Here's the code: 这是代码:
<p><a href="http://foo.com/foo.html"></a>
<a class="foo_blog" href="http://foo.com" target="_blank">bar</a>
:</p>
I want to find and remove any <p>
that has an <a>
with the class "foo_blog". 我想查找并删除具有
<a>
类“ foo_blog”的任何<p>
。
I do not have control over the HTML—it's coming from elsewhere. 我无法控制HTML,它来自其他地方。
这应该做
$('p>a.foo_blog').remove();
You don't specify whether the anchor element has to be a direct child of the paragraph or can be further down the tree, so here are some solutions to cover either case. 您无需指定anchor元素是该段落的直接子元素还是可以在该树的更下一层,因此这里提供了一些解决这两种情况的解决方案。 All three will work for the example html you showed.
这三个都将适用于您显示的示例html。
$("p").has("a.foo_blog").remove();
// OR
$("a.foo_blog").closest("p").remove();
// OR
$("a.foo_blog").parent("p").remove();
The first finds all paragraph elements that have a descendant that is an anchor with that class, and removes such paragraphs. 第一个查找具有后代的所有段落元素,该后代是该类的锚点,然后删除此类段落。
The second finds the anchors and then goes up to their nearest containing paragraph and removes it. 第二个找到锚点,然后向上移动到最接近的包含段并将其删除。
The last finds the anchors and removes the immediate parent element if and only if it is a paragraph element. 当且仅当它是一个段落元素时,最后一个查找锚点并删除直接父元素。
Note that when the paragraphs in question are removed their child element(s) will be removed too. 请注意,当删除相关段落时,其子元素也将被删除。
(The jQuerymethods I used are relatively self explanatory, but for more detail about them you know where to look .) (我使用的jQuery方法相对来说很容易说明,但是有关它们的更多详细信息,您知道应该在哪里看 。)
$("a.foo_blog, p.foo_blog").remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.