繁体   English   中英

使用jquery检查li中是否存在特定文本

[英]check if specific text exists inside li using jquery

我有以下正在呈现的 html,我想检查 li 中是否有特定文本(所有文档、请求和付款) 我的 html 是

<div class="msos-selecteditems-container" tabindex="0" style="background-color: rgb(255, 255, 0);" 
 title="All documentation, requests and payments, Final documentation">
<ul class="msos-selecteditems msos-current-selection-normal" tabindex="-1" 
 aria-label="Selected values for Student Responsibilities" 
aria-activedescendant="value-selected-item-1">
 <li class="msos-selected-display-item msos-visible msos-selecteditem-active" 
  name="value-selected-item-1" aria-describedby="value_selecteditems_instructions" 
  id="value-selected-item-1" 
  tabindex="-1" 
  title="" data-value="0" 
  aria-label="All documentation, requests and payments for Student Responsibilities">
 <span class="msos-selected-display-item-text">All documentation, requests and payments</span>
 <button class="msos-quick-delete" tabindex="-1" name="value-selected-item-delete-1" aria- 
  label="Remove All documentation, requests and payments">
  <span class="msos-glyph"></span></button>
</li>
<li class="msos-selected-display-item msos-visible" name="value-selected-item-2" 
  aria-describedby="value_selecteditems_instructions" 
  id="value-selected-item-2" 
  tabindex="-1" 
  title="" 
  data-value="1" aria-label="Final documentation for Student Responsibilities">
  <span class="msos-selected-display-item-text">Final documentation</span>
  <button class="msos-quick-delete" tabindex="-1" name="value-selected-item-delete-2" 
   aria-label="Remove Final documentation">
  <span class="msos-glyph"></span></button>
 </li>
</ul>
<div class="msos-selecteditems-toggle-container">
<button class="msos-selecteditems-toggle msos-invisible" tabindex="-1" aria-hidden="true" type="button" aria-label="Show 2 more selected items">+2</button>
</div>
</div>

这是我想要做的,但它总是返回 true ,这是不正确的

   if($('span:contains("All documentation, requests and payments")','ul.msos-selecteditems msos-current-selection-normal').length == 0) {
    console.log('Found');
    } else {
    console.log('no Found');
    }

您忘记了两个类之间的一个点: $('span:contains("All documentation, requests and payments")','ul.msos-selecteditems . msos-current-selection-normal') -

 console.log('Found elements:', Array.from($('span:contains("All documentation, requests and payments")','ul.msos-selecteditems.msos-current-selection-normal')))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="msos-selecteditems-container" tabindex="0" style="background-color: rgb(255, 255, 0);" title="All documentation, requests and payments, Final documentation"> <ul class="msos-selecteditems msos-current-selection-normal" tabindex="-1" aria-label="Selected values for Student Responsibilities" aria-activedescendant="value-selected-item-1"> <li class="msos-selected-display-item msos-visible msos-selecteditem-active" name="value-selected-item-1" aria-describedby="value_selecteditems_instructions" id="value-selected-item-1" tabindex="-1" title="" data-value="0" aria-label="All documentation, requests and payments for Student Responsibilities"> <span class="msos-selected-display-item-text">All documentation, requests and payments</span> <button class="msos-quick-delete" tabindex="-1" name="value-selected-item-delete-1" aria- label="Remove All documentation, requests and payments"> <span class="msos-glyph"></span></button> </li> <li class="msos-selected-display-item msos-visible" name="value-selected-item-2" aria-describedby="value_selecteditems_instructions" id="value-selected-item-2" tabindex="-1" title="" data-value="1" aria-label="Final documentation for Student Responsibilities"> <span class="msos-selected-display-item-text">Final documentation</span> <button class="msos-quick-delete" tabindex="-1" name="value-selected-item-delete-2" aria-label="Remove Final documentation"> <span class="msos-glyph"></span></button> </li> </ul> <div class="msos-selecteditems-toggle-container"> <button class="msos-selecteditems-toggle msos-invisible" tabindex="-1" aria-hidden="true" type="button" aria-label="Show 2 more selected items">+2</button> </div> </div>

使用$('li').attr("aria-label")您可以从aria-label获取包含文本的字符串,然后您可以查看该字符串是否包含您想要的内容。

例子:

if ($('li').attr("aria-label").indexOf("All documentation, requests and payments")!=-1) {
  console.log('Found');
} else {
  console.log('no Found');
}

暂无
暂无

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

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