简体   繁体   English

jQuery - 隐藏具有包含特定文本的span元素的DIV

[英]jQuery - Hide DIV which has a span element with certain text inside

I have several divs with the same class names and varying IDs. 我有几个具有相同类名和不同ID的div。 The ID is not set for the text I need to target 我没有为我需要定位的文本设置ID

I need to target the Telephone Call text. 我需要定位电话呼叫文本。 If the div contains that text, how do I hide the containing div 如果div包含该文本,我如何隐藏包含div

<div id="rn_FieldDisplay_155" class="rn_FieldDisplay rn_Output">
   <span class="rn_DataLabel">Telephone Call </span>
   <div class="rn_DataValue">No</div>
</div>

I have tried the following to no avail 我试过以下无济于事

<script>
$(document).ready(function() {
  $(".rn_FieldDisplay > span:contains('Telephone Call')").hide ();
});
</script>

If your code is hiding the span, but not the parent div, you can target the div to be hidden using mostly the same code you already wrote. 如果您的代码隐藏了span,而不是父div,则可以使用与已编写的代码相同的代码来隐藏div。

<script>
$(document).ready(function() {
    $(".rn_FieldDisplay > span:contains('Telephone Call')").parent().hide();
});
</script>

Try selecting it's child instead of the element itself : 尝试选择它的孩子而不是元素本身:

$(document).ready(function() {
    $(".rn_FieldDisplay *:contains('Telephone Call')").hide ();
});

Try with find() function 尝试使用find()函数

 $(document).ready(function() { $(".rn_FieldDisplay").find(":contains('Telephone Call')").hide (); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="rn_FieldDisplay_155" class="rn_FieldDisplay rn_Output"> <span class="rn_DataLabel">Telephone Call </span> <div class="rn_DataValue">No</div> <p class="rn_DataLabel">Telephone Call </p> </div> 

Well you can try this: 那么你可以试试这个:

if ($(".rn_FieldDisplay > span:contains('Telephone Call')").length > 0) {
    $(".rn_FieldDisplay > span").hide();
}

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

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