如何检查div中是否存在fa关键字?
例如,这是我的HTML,
<div class="item">
<div class="title">How to check if a keyword exists</div>
<div class="author">John J, 1990</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我试过下面这个方法,但显然不起作用!
if ($('.item .author').text('John J').length > 0)
{
alert('found John J');
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢.
Nic*_*ver 13
您可以使用:contains选择器,如下所示:
if ($('.item .author:contains("John J")').length > 0)
{
alert('found John J');
}
Run Code Online (Sandbox Code Playgroud)
...或者你最终可能会追求的东西,像这样:
$('.item .author:contains("John J")').addClass("highlight");
Run Code Online (Sandbox Code Playgroud)
通过呼叫.text()您正在设置文本,而不是检查它.使用.text()的搜索应该是这样的:
if ($('.item .author').text().indexOf("John J") != -1)
{
alert('found John J');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11273 次 |
| 最近记录: |