简体   繁体   English

如果字符串参数等于div的文本,如何隐藏最接近的标签

[英]How to hide closest label If string parameter equals text of a div

If the text in div.level equals the text in variable dlevel I would like to hide the closest label to that div. 如果div.level的文本等于变量dlevel的文本,我想隐藏最接近该div的标签。

I cannot directly edit the HTML, it is automatically created by the software so I cannot edit the text in div.level. 我无法直接编辑HTML,它是由软件自动创建的,因此无法编辑div.level中的文本。 The variable "dlevel" is actually being pulled in from a URL parameter. 实际上,变量“ dlevel”是从URL参数中提取的。 I can change this parameter (the value of 'dlevel') as it may be an issue with it containing '$'. 我可以更改此参数(“ dlevel”的值),因为它包含“ $”可能是一个问题。 However, this needs to be versatile so that if the parameter is 50 the label for 50 is hidden and not the label for 500 and vice versa. 但是,这需要通用,以便如果参数为50,则隐藏50的标签,而不隐藏500的标签,反之亦然。 Any help would be greatly appreciated. 任何帮助将不胜感激。

 var dlevel = '$50'; $('div.level').filter(function () { return $(this).text() == dlevel; }).closest('label').hide(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <div class="level-container"> <label> <div class="level"> $50 </div> </label> </div> <div class="level-container"> <label> <div class="level"> $100 </div> </label> </div> <div class="level-container"> <label> <div class="level"> $500 </div> </label> </div> 

I think you are using the wrong approach. 我认为您使用了错误的方法。

You can loop through the divs .level using jQuery each() and get the textContent of each div. 您可以使用jQuery each() .level并获取每个div的textContent

Then all you need to do is the check if the text equals to the value of dlevel , if yes, get the closest label with closest() (that traverse up the DOM) and hide it. 然后,您所需要做的就是检查文本是否等于dlevel的值,如果是,则使用dlevel closest() (遍历DOM)获取最接近的标签并将其隐藏。

 var dlevel = '$50'; $('div.level').each(function(){ let text = this.textContent.trim(); //or this.innerText.trim() if (text == dlevel){ $(this).closest('label').hide() } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <div class="level-container"> <label> <div class="level"> $50 </div> </label> </div> <div class="level-container"> <label> <div class="level"> $100 </div> </label> </div> <div class="level-container"> <label> <div class="level"> $500 </div> </label> </div> 

尝试$(this).text()。trim()=== dlevel

Try using a each() like this. 尝试像这样使用each() You also need to use trim() to remove the whitespace. 您还需要使用trim()删除空格。 You can use parent() to find the label parent of each .level as long as the label is always the parent. 您可以使用parent()查找每个.level的标签父.level ,只要标签始终是父级。

 let dlevel = "$50"; $('div.level').each( function() { if ($(this).text().trim() === dlevel) { $(this).parent().hide() } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <div class="level-container"> <label> <div class="level"> $50 </div> </label> </div> <div class="level-container"> <label> <div class="level"> $100 </div> </label> </div> <div class="level-container"> <label> <div class="level"> $500 </div> </label> </div> 

Try Following using $.each 尝试跟随$.each

  var dlevel = "$50"; $.each($('div .level'),function(i,val){ if($(val).text().trim()==dlevel) { $(val).closest('label').hide(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <div class="level-container"> <label> <div class="level"> $50 </div> </label> </div> <div class="level-container"> <label> <div class="level"> $100 </div> </label> </div> <div class="level-container"> <label> <div class="level"> $500 </div> </label> </div> 

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

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