[英]Check for a specific character in a string
How can I check for a specific character in a element? 如何检查元素中的特定字符?
<div>N0180/RIB</div>
<div>N0180918</div>
<div>N0180</div>
**(I want to target <div>N0180</div> this element)**
I tried to use :contains("N0180")
and .substring
method. 我尝试使用
:contains("N0180")
和.substring
方法。 But it is target all three elements. 但这是所有这三个要素的目标。
As per :contains()
Doc: 按照
:contains()
Doc:
The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof.
匹配的文本可以直接出现在所选元素中,该元素的任何后代或其组合中。 As with attribute value selectors, text inside the parentheses of :contains() can be written as a bare word or surrounded by quotation marks.
与属性值选择器一样,:contains()括号内的文本可以写为裸词,也可以用引号引起来。 The text must have matching case to be selected.
文本必须具有匹配的大小写才能选择。
You can rather use .filter() for more specific comparison : 您可以使用.filter()进行更具体的比较:
$('div').filter(function(){
return $(this).text().trim() === "N0180";
})
For exact match use .filter()
对于完全匹配,请使用
.filter()
$('div').filter(function(){
return $(this).text().trim() == "N0180";
})
Note: For case-insensitive search convert the values to same cases using toLowerCase()
or .toUpperCase()
注意:对于不区分大小写的搜索,请使用
toLowerCase()
或.toUpperCase()
将值转换为相同的大小写
$('div').each(function() { if ($(this).text() === 'N0180') { $(this).addClass('red') } })
.red { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>N0180/RIB</div> <div>N0180918</div> <div>N0180</div>
You can get the .text()
of div and compare to your text 您可以获取div的
.text()
并与您的文本进行比较
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.