简体   繁体   English

隐藏包含的div <p> 包含“文本” - 如何

[英]hide div that contains <p> that contains “text” - how to

<div id="example">
 <ul>
  <p>text</p>
 </ul>
<div>

i need to hide div itself 我需要隐藏div本身

Original Answer: 原答案:

$("div p:contains('text'))").hide(); $(“div p:contains('text'))”)。hide();

This won't work because it hides the <p> and not the <div> . 这不起作用,因为它隐藏了<p>而不是<div> We also need to employ :has . 我们还需要雇用:has

$("div:has(p:contains('text'))").hide();

Demo here 在这里演示

The solutions with parent() can't guarantee that the right parent is being used. parent()的解决方案无法保证正在使用正确的父级。

Use this: 用这个:

$('div').has("p:contains('text')").hide();
$("div p:contains('text')").closest("div").hide();

工作解决方案: http//jsfiddle.net/7qmwj/8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM