Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

jQuery or JavaScript equivalent of PHP strpos function to find string on a page

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
question from:https://stackoverflow.com/questions/3978204/jquery-or-javascript-equivalent-of-php-strpos-function-to-find-string-on-a-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


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")');

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...