Home  >  Article  >  Web Front-end  >  How does jquery find the string of the current page and locate that string position? _html/css_WEB-ITnose

How does jquery find the string of the current page and locate that string position? _html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:44:492001browse

How does jquery find the string of the current page and locate that string position? If it cannot be found, it will prompt not found. It's a bit similar to the Ctrl f function of a browser. Please give me a solution. Thank you all. I wrote one before, but it is not compatible with Google Chrome


Reply to the discussion (solution)

Directly indexOf the document

Directly indexOf the document



How to position it?


Directly indexOf the document



How to position it?
Change the color of the search element after searching. That’s it

                                                                                                                                                                                   ();
var searchText = $("#keyword").val();
var regExp = new RegExp(searchText, 'g'); //Create a regular expression, g represents global, if If g is not used, the search will not continue after finding the first one; 🎜> var newHtml = html.replace(regExp, '' searchText ''); //Replace the found keywords and add the highlight attribute;

                                                                                                                                               var ele = $(".highlight"). eq(0);
var top = ele.offset().top;
$('html,body').animate({ scrollTop: top }, 300); > })

function clearSelection() {
$('p').each(function () {
//Find all elements with highlight attribute;
$(this).find ('.highlight').each(function () {
                                                                                                                                                                           } );

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn