【发布时间】: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