jQuery或与PHP strpos函数等效的JavaScript,可在页面上查找字符串 [英] jQuery or JavaScript equivalent of PHP strpos function to find string on a page
问题描述
JavaScript或jQuery中是否有与PHP中的strpos
类似的等效功能?
Is there an equivalent function in JavaScript or jQuery similar to strpos
in PHP?
我想在页面上的元素内找到一个字符串.我要查找的字符串是:
I want to locate a string inside an element on a page. The string I'm looking for is:
td class="SeparateColumn"
我想要可以在其中运行它的东西来找到它
I would like something where I can run it like this to find:
if $("anystring")
then do it
推荐答案
我假设您的意思是检查字符串是否包含字符以及字符串在字符串中的位置-您想使用字符串的indexOf()
方法在JS中.这是相关文档.
I assume you mean check whether a string contains a character, and the position in the string - you'd like to use the indexOf()
method of a string in JS. Here are the relevant docs.
好的,所以您想搜索整个页面! :contains()
选择器将执行此操作.有关:contains
,请参见 jQuery文档.
Okay, so you'd like to search the whole page! The :contains()
selector will do that. See the jQuery docs for :contains
.
要搜索页面中的每个元素,请使用
To search every element in the page, use
var has_string = $('*:contains("search text")');
如果您返回了jQuery元素,则搜索成功.例如,在此页面上
If you get jQuery elements back, then the search was a success. For example, on this very page
var has_string=$('*:contains("Alex JL")').length
//has_string is 18
var has_string=$('*:contains("horsey rodeo")').length
//has_string if 0. So, you could an `if` on this and it would work as expected.
这篇关于jQuery或与PHP strpos函数等效的JavaScript,可在页面上查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!