jQuery在元素中搜索文本作为SQL查询LIKE? [英] jQuery search text in elements as SQL query LIKE?

查看:89
本文介绍了jQuery在元素中搜索文本作为SQL查询LIKE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,无论如何在dom中搜索文本,就像我们为SQL查询LIKE做的那样?

Hi is there anyway to search text in dom, as we do for SQL query LIKE?

我的意思是,我有。

<ul>
<li>ab</li>
<li>abrecot</li>
<li>abus</li>
<li>aby</li>
<li>abrea</li>
</ul>

我想搜索abre,所以以文本ASC顺序返回:

i would like to search for "abre" and so return in text ASC order:

<li>abrea</li>
<li>abrecot</li>

这可能吗?

绝对是查询看起来像:

SELECT <li> FROM <ul> WHERE text LIKE 'abre%' ORDER BY text ASC; :))


推荐答案

当您在寻找文本元素时指定字符串开头,您可以使用过滤器方法:

As you are looking for elements whose text starts with a specified string, you can use the filter method:

var x = $("ul li").filter(function() {
    return $(this).text().substr(0, 4) === "abre";
});

这将只返回开头字符串为abre的元素。使用包含方法的其他答案将返回在其中任何位置找到的字符串的元素,这与您的伪SQL查询不匹配。

This will only return elements which have the string "abre" at the start. The other answers using the contains method will return elements with the string found anywhere within them, which does not match your pseudo-SQL query.

以上是工作示例

这篇关于jQuery在元素中搜索文本作为SQL查询LIKE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆