Javascript window.getSelection - PullRequest
       1

Javascript window.getSelection

0 голосов
/ 09 декабря 2011

У меня есть следующий скрипт, но он работает только в IE, я бы хотел, чтобы он поддерживал и другие браузеры (Chrome, Firefox), но не знаю, как это сделать.Скрипт для поиска по странице Полный скрипт здесь: http://goo.gl/aEk3r - Документы Google

function performMultiSearch(elem,searchElem) {
// set up variables
var searchString; // Will hold the text to search for
var theSelection; // Will hold the document's selection object
var textNodes; // Will hold all the text nodes in the document

// Set it to search the entire document if we haven't been given an element to search
if(!searchElem || typeof(searchElem) == 'undefined') searchElem = document.body;

// Get the string to search for
if(elem && elem.value) searchString = elem.value;
else if(this && this.value) searchString = this.value;

// Get all the text nodes in the document
textNodes = findTypeNodes(searchElem,3);

// Get the selection object
if(window.getSelection) theSelection = window.getSelection(); // firefox
else { // some other browser - doesn't support multiple selections at once
    alert("sorry this searching method isn't supported by your browser");
    return;
}

// Empty the selection
theSelection.removeAllRanges(); // We want to empty the selection regardless of whether we're selecting anything

большое спасибо

1 Ответ

0 голосов
/ 09 декабря 2011

Сценарий отсюда

http://www.javascriptsource.com/miscellaneous/search-the-page.html

Эта часть кода не работает в Fx

var reSearch = new RegExp(searchString,'gmi'); // Set it to 'g' - global (finds all instances), 'm' - multiline (searches more than one line), 'i' - case insensitive
       var stringToSearch = textNodes[i].textContent;
       while(reSearch(stringToSearch)) { // While there are occurrences of the searchString

, так как reSearch не является функцией.

Если у меня есть время, я могу посмотреть, как это исправить.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...