Javascript window.getSelection

时间:2011-12-09 08:56:25

标签: javascript search google-chrome

我得到了以下脚本,但它只适用于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 :(得分:0)

脚本来自这里

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不是函数。

如果我有时间,我可以看看如何解决它。