简体   繁体   English

jQuery在元素中搜索文本作为SQL查询LIKE?

[英]jQuery search text in elements as SQL query LIKE?

Hi is there anyway to search text in dom, as we do for SQL query LIKE? 嗨,无论如何在dom中搜索文本,就像我们为SQL查询LIKE做的那样?

i mean, i have. 我的意思是,我有。

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

i would like to search for "abre" and so return in text ASC order: 我想搜索“abre”,所以返回ASC文本顺序:

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

is this possible? 这可能吗?

definitely the query would looks like doing: 绝对的查询看起来像:

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: 在查找文本指定字符串开头的元素 ,可以使用filter方法:

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

This will only return elements which have the string "abre" at the start. 这将仅返回在开头具有字符串“abre”的元素。 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. 使用contains方法的其他答案将返回在其中任何位置找到的字符串的元素,这与您的伪SQL查询不匹配。

Here's a working example of the above. 这是上面的一个工作示例

I think you would want the jQuery 'contains' function, have a look at the docs here: 我想你会想要jQuery'contains'函数,看看这里的文档:

http://api.jquery.com/contains-selector/ http://api.jquery.com/contains-selector/

Your example would probably look like this: 您的示例可能如下所示:

$("li:contains('abre')")

EDIT to include the comments here, if you are looking for "starts with", you can do this on an element using the following syntax: 编辑以包含此处的注释,如果您正在寻找“以...开头”,您可以使用以下语法对元素执行此操作:

$('li[anAttribute^="abre"]')

But this assumes you have an attribute to query which i don't think you do, in which case the filters answer will likely suit your needs best. 但这假设您有一个属性来查询我认为您不会做的事情,在这种情况下,过滤器的答案可能最适合您的需求。

The :contains() selector is probably what you're looking for: :contains()选择器可能就是你要找的东西:

http://api.jquery.com/contains-selector/ http://api.jquery.com/contains-selector/

QUOTE: 引用:

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<div>John Resig</div>

<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>


<script>
$("div:contains('John')").css("text-decoration", "underline");
</script>

</body>
</html>

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

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