简体   繁体   English

如何在具有jQuery的UL中查找具有特定内容的LI?

[英]How to find a LI with particular content in an UL with jQuery?

In the following UL with id of CP_main_approvalist I want to find the LI that has " Awating authorisation " string and to extract that text from it: 在下面的ID为CP_main_approvalist UL ,我想找到具有“ Awating授权 ”字符串的LI并从中提取该文本:

在此处输入图片说明

There can be many LI s in that UL . UL可以有许多LI In this case there are only two. 在这种情况下,只有两个。

You can access it by: $("#CP_Main_approvalList li:contains('Awating authorisation')") , 您可以通过以下方式访问它: $("#CP_Main_approvalList li:contains('Awating authorisation')")

So something like this: 所以像这样:

 $("#CP_Main_approvalList li:contains('Awating authorisation')").css('color', 'red'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul id="CP_Main_approvalList"> <li>1. Authorised by...</li> <li>2. Awating authorisation from....</li> </ul> 

To extract the text from the specified LI you will have to call a text() method on the resulting set: 要从指定的LI提取文本,您将必须在结果集上调用text()方法:

$("#CP_Main_approvalList li:contains('Awating authorisation')").text()

But you have to be careful, since if there is more than one LI with such text, it will extract it from all the matching elements. 但是您必须小心,因为如果有多个带有此类文本的LI ,它将从所有匹配的元素中提取出来。 In order to extract only from the first one use: 为了仅从第一个用途中提取:

$("#CP_Main_approvalList li:contains('Awating authorisation')").first().text()

Or :eq(idx) ( idx is 0-based) selector, instead of first() to pick the element with some other index. 或者:eq(idx)idx从0开始)选择器,而不是first()来选择具有其他索引的元素。

UPDATE 更新

So far it was the direct answer to your question, but in general this technique is far from the best practice, provides bad performance (query selector has to look inside every LI and match the actual text) and is prone to potential future errors (what if someone decides to change that text - the whole extraction logic will break). 到目前为止,这是您问题的直接答案,但总的来说,该技术与最佳实践相去甚远,性能差(查询选择器必须查看每个LI并匹配实际文本),并且容易出现潜在的未来错误(如果有人决定更改该文本-整个提取逻辑将中断)。 Consider adding appropriate class name or data attribute to the corresponding elements ( authorized and authorizing for example) and differentiate between them by it. 考虑将适当的类名称或数据属性添加到相应的元素(例如,已authorizedauthorizing ),并通过它们进行区分。

You can try this by using children or has function like below: 您可以通过使用子项或具有如下功能的方法进行尝试:

$("#CP_Main_approvalList").children("li contains('Awating authorisation')").prop('innerHTML');

OR 要么

$("#CP_Main_approvalList").has("li contains('Awating authorisation')").prop('innerHTML');

You can select the ul by using the id selector: 您可以使用id选择器来选择ul

$("#CP_Main_approvalList")

Then, add the li and make sure it contains the text you need 然后,添加li并确保它包含您需要的文本

li:contains('Awaiting authorisation')

And to make sure it works I changed the text color... 为了确保它有效,我更改了文字颜色...

 $("#CP_Main_approvalList li:contains('Awaiting authorisation')").css("color", "green"); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul id="CP_Main_approvalList"> <li>1. Authorised by ..... </li> <li>2. Awaiting authorisation from ..... </li> </ul> 

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

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