【问题标题】:Javascript window.getSelectionJavascript window.getSelection
【发布时间】:2011-12-09 08:56:24
【问题描述】:

我得到了以下脚本,但它只适用于 IE,我希望它也支持其他浏览器(Chrome、Firefox),但不知道该怎么做。脚本用于在页面中搜索 完整的脚本在这里:http://goo.gl/aEk3r - Google Docs

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

非常感谢

【问题讨论】:

  • 你为什么不告诉我们你想做什么——我们可以尝试通过阅读代码来推断它,但如果你真的说出来它更有用。
  • 对不起,你的脚本太大了。我们不喜欢调试你的代码。(你可以在firefox中测试,使用firebug控制台调试你的代码)

标签: javascript search google-chrome


【解决方案1】:

脚本来自这里

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

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

【讨论】:

    猜你喜欢
    • 2015-01-14
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2015-11-17
    相关资源
    最近更新 更多