简体   繁体   中英

How to limit Javascript's window.find to a particular DIV?

Is it possible to use Javascript in Safari/Firefox/Chrome to search a particular div container for a given text string. I know you can use window.find(str) to search the entire page but is it possible to limit the search area to the div only?

Thanks!

Once you look up your div (which you might do via document.getElementById or any of the other DOM functions, various specs here ), you can use either textContent or innerText to find the text of that div . Then you can use indexOf to find the string in that.

Alternately, at a lower level, you can use a recursive function to search through all text nodes in the window, which sounds a lot more complicated than it is. Basically, starting from your target div (which is an Element ), you can loop through its childNodes and search their nodeValue string (if they're Text s) or recurse into them (if they're Element s).

The trick is that a naive version would fail to find "foo" in this markup:

<p><span>fo</span>o</p>

...since neither of the two Text nodes there has a nodeValue with "foo" in it (one of them has "fo" , the other "o" ).

Depending on what you are trying to do, there is an interesting way of doing this that does work (does require some work).

First, searching starts at the location where the user last clicked. So to get to the correct context, you can force a click on the div. This will place the internal pointer at the beginning of the div.

Then, you can use window.find as usual to find the element. It will highlight and move toward the next item found. You could create your own dialog and handle the true or false returned by find, as well as check the position. So for example, you could save the current scroll position, and if the next returned result is outside of the div, you restore the scroll. Also, if it returns false, then you can say there were no results found.

You could also show the default search box. In that case, you would be able to specify the starting position, but not the ending position because you lose control.

Some example code to help you get started. I could also try putting up a jsfiddle if there is enough interest.

Syntax:

window.find(aStringToFind, bCaseSensitive, bBackwards, bWrapAround, bWholeWord, bSearchInFrames, bShowDialog);

For example, to start searching inside of myDiv, try

document.getElementById("myDiv").click(); //Place cursor at the beginning
window.find("t", 0, 0, 0, 0, 0, 0); //Go to the next location, no wrap around

You could set a blur (lose focus) event handler to let you know when you leave the div so you can stop the search.

To save the current scroll position, use document.body.scrollTop. You can then set it back if it trys to jump outside of the div.

Hope this helps! ~techdude

As per the other answer you won't be able to use the window.find functionality for this. The good news is, you won't have to program this entirely yourself, as there nowadays is a library called rangy which helps a lot with this. So, as the code itself is a bit too much to copy paste into this answer I will just refer to a code example of the rangy library that can be found here . Looking in the code you will find

 searchScopeRange.selectNodeContents(document.body);

which you can replace with

 searchScopeRange.selectNodeContents(document.getElementById("content"));

To search only specifically in the content div.

var elements = [];
$(document).find("*").filter(function () {
  if($(this).text().contains(yourText))
    elements.push($(this));
});
console.log(elements);

I didn't try it, but according the jQuery documentation it should work.

Here is how I am doing with jquery:

var result = $('#elementid').text().indexOf('yourtext') > -1

it will return true or false

If you are still looking for someting I think I found a pretty nice solution;

Here it is : https://www.aspforums.net/Threads/211834/How-to-search-text-on-web-page-similar-to-CTRL-F-using-jQuery/

And I'm working on removing jQuery (wip) : codepen.io/eloiletagant/pen/MBgOPB Hope it's not too late :)

也许你试图不使用jquery ...但如果没有,你可以使用这个$('div:contains(whatyouarelookingfor)')唯一的问题是它可以返回同样包含匹配的子div的父元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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