简体   繁体   English

我如何在此使用.remove <p> 有<a>特定类别的?</a>

[英]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.

相关问题 如何使用querySelector删除“ p”标签 - How do I remove 'p' tags with querySelector 如何使用 jquery 删除特定的 CSS 类? - How do I remove a specific CSS class with jquery? 如何在ap类中检查特定内容并在html文件中使用javascript创建if语句? - How do I check for specific content in a p class and create an if statement based on that with javascript in an html file? 如何使用JavaScript注入单击特定类的链接? - How do I use a javascript injection to click on links of a specific class? 我该如何移除 <p> 在Rails 4中使用jQuery标签 - how do i remove <p> tag using jquery in rails 4 如何从$(&#39;p&#39;)集合中删除所有类? - How do I remove all classes from a collection of $('p')? 使用 JavaScript,我将如何编写“如果下一个兄弟姐妹具有特定的 class 名称 xxx,则执行此操作,否则执行此操作”? - Using JavaScript, how would I write 'if the next sibling has the specific class name xxx, do this, else do that'? 如何在ap的特定行上放置div? - How do I put a div over a specific line of a p? 如何在课程中添加/删除课程 <div> 当它已经有课程时 - How do I add/remove a class in a <div> when it already has classes 如何使用 jQuery 切换 class 添加 class 或删除元素? - How do I use jQuery toggle class to add class or remove element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM