如何在li> div中找到特定字符串并将其隐藏

时间:2016-08-24 12:25:38

标签: javascript jquery css

我有一个ul列表,里面有li,而li里面有div,没有class和id。我只是想找到li> div,其中有文字说'Certificats et forfaits'并隐藏它旁边的锚标签。有可能吗?

这是我的代码

<ul class="select2-choices">  
    <li class="select2-search-choice">    
        <div>Informatique Électronique</div>    
        <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
    </li>
    <li class="select2-search-choice">   
        <div>Informatique Électronique &gt; Accessoires Informatique</div>
        <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
    </li>
    <li class="select2-search-choice">    
        <div>Mode et Accessoires</div>    
        <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
    </li>
    <li class="select2-search-choice">    
        <div>Certificats et forfaits</div>    
        <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
    </li>
    <li class="select2-search-field">    
        <label for="s2id_autogen3" class="select2-offscreen"></label>    
        <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen3" placeholder="" style="width: 20px;">  
    </li>
</ul>

在我上面的列表中,我想找到文本Certificats et forfaits,它总是在div中,它总是在li之后,一旦我发现我需要隐藏它旁边的锚标记。我怎样才能实现这一目标呢?

5 个答案:

答案 0 :(得分:2)

请查看下面的代码段。

&#13;
&#13;
08:30:34,873 WARN  [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (ServerService Thread Pool -- 4) HHH000342: Could not obtain connection to query metadata : Cannot 
create JDBC driver of class '' for connect URL 'jdbc:hsqldb:mem:test'
08:30:34,876 WARN  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 4) Exception encountered during context initialization 
- cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ca.uhn.fhir.jpa.demo.FhirServerCo
nfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalCo
ntainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested serv
ice [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
&#13;
$( document ).ready(function() {
  $('ul li').each(function(){
    if($(this).children('div').text() == 'Certificats et forfaits'){
      $(this).children('a').hide();
    }
  });
});
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个:使用.filter()获取所需的div然后隐藏下一个锚

&#13;
&#13;
$(function(){
   $('ul li>div').filter(function(){
      return $(this).text() == 'Certificats et forfaits';
    }).next().hide();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="select2-choices">  

<li class="select2-search-choice">    
<div>Informatique Électronique</div>    
<a href="#" class="select2-search-choice-close" tabindex="-1">Informatique Électronique</a>
</li>

<li class="select2-search-choice">   
<div>Informatique Électronique &gt; Accessoires Informatique</div>
<a href="#" class="select2-search-choice-close" tabindex="-1">Informatique Électronique &gt; Accessoires Informatique</a>
</li>

<li class="select2-search-choice">    
<div>Mode et Accessoires</div>    
<a href="#" class="select2-search-choice-close" tabindex="-1">Mode et Accessoires</a>
</li>

<li class="select2-search-choice">    
<div>Certificats et forfaits</div>    
<a href="#" class="select2-search-choice-close" tabindex="-1">Certificats et forfaits</a>
</li>

<li class="select2-search-field">    
<label for="s2id_autogen3" class="select2-offscreen"></label>    
<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen3" placeholder="" style="width: 20px;">  
</li>

</ul>
&#13;
&#13;
&#13;

编辑 - 要隐藏特定的锚标记,可以在父li中找到锚,如下所示

$(function(){
       $('ul li>div').filter(function(){
          return $(this).text() == 'Certificats et forfaits';
        }).parent().find('a.select2-search-choice-close').hide();
    });

答案 2 :(得分:1)

使用Contains selector

 $( "ul li>div:contains('Certificats et forfaits')" ).hide();

隐藏其锚点

$( "ul li>div:contains('Certificats et forfaits')" ).siblings("a").hide();

Working Demo

答案 3 :(得分:1)

可以.each()jquery

一起使用
$("li").each(function(){
    if($(this).find("div").html() == "Certificats et forfaits"){
       $(this).next().hide();
    }
});

fiddle example

答案 4 :(得分:1)

如果您知道列表中的哪个号码需要隐藏,那么您可以使用:nth-child(#)选择器。以下是根据您提供的代码隐藏锚标记的方法:

.select2-search-choice:nth-child(4) a {
  display: none;
  }