简体   繁体   English

jQuery .search()到任何字符串

[英]jQuery .search() to any string

I saw this code snippet: 我看到了这段代码:

$("ul li").text().search(new RegExp("sometext", "i"));

and wanted to know if this can be extended to any string? 并想知道这是否可以扩展到任何字符串?

I want to accomplish the following, but it dosen't work: 我想完成以下任务,但它不起作用:

$("li").attr("title").search(new RegExp("sometext", "i"));

Also, anyone have a link to the jQuery documentation for this function? 此外,任何人都有这个功能的jQuery文档的链接? I fail at googling apparently. 我显然在Google上搜索失败了。

search() is a String method. search()是一个String方法。

You are executing the attr function on every <li> element. 您正在每个 <li>元素上执行attr函数。 You need to invoke each and use the this reference within. 您需要调用each并在其中使用this引用。

Example: 例:

$('li').each(function() {
    var isFound = $(this).attr('title').search(/string/i);
    //do something based on isFound...
});
if (str.toLowerCase().indexOf("yes") >= 0)

要么,

if (/yes/i.test(str))

Ah, that would be because RegExp is not jQuery. 啊,那是因为RegExp不是jQuery。 :) :)

Try this page . 试试这个页面 jQuery.attr doesn't return a String so that would certainly cause in this regard. jQuery.attr不返回String,因此肯定会导致这方面的问题。 Fortunately I believe you can just use .text() to return the String representation. 幸运的是,我相信你可以使用.text()来返回String表示。

Something like: 就像是:

$("li").val("title").search(/sometext/i));

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

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