【问题标题】:search page function for internet explorerInternet Explorer 的搜索页面功能
【发布时间】:2012-10-02 10:30:06
【问题描述】:

我需要在我正在处理的网站中添加搜索页面功能。我在网上找到了一个,它在 Firefox 和 Chrome 中运行良好,但在 IE 中根本不行。我认为我没有编写此代码的事实使其特别难以调试。任何帮助或指导表示赞赏!

HTML

<form id="f1" name="f1" action="javascript:void(0)" onsubmit="searchpage()" >
<input id="t1" type="text" name="t1" />
<input id="button" type="submit" value="FIND" name="b1" onclick="searchpage()" />
</form>

JAVASCRIPT

function searchpage() {
    if (document.getElementById("t1").value != null && this.document.getElementById("t1").value != '') parent.findString(document.getElementById("t1").value);
    return false;
}
var TRange = null;

function findString(str) {
    if (parseInt(navigator.appVersion) < 4) return;
    var strFound;
    if (window.find) {
        // CODE FOR BROWSERS THAT SUPPORT window.find
        strFound = self.find(str);
        if (!strFound) {
            strFound = self.find(str, 0, 1);
            while (self.find(str, 0, 1)) continue;
        }
    }
    else if (navigator.appName.indexOf("Microsoft") != -1) {
        // EXPLORER-SPECIFIC CODE
        if (TRange != null) {
            TRange.collapse(false);
            strFound = TRange.findText(str);
            if (strFound) TRange.select();
        }
        if (TRange == null || strFound == 0) {
            TRange = self.document.body.createTextRange();
            strFound = TRange.findText(str);
            if (strFound) TRange.select();
        }
    }
    else if (navigator.appName == "Opera") {
        alert("Opera browsers not supported, sorry...")
        return;
    }
    if (!strFound) alert("String '" + str + "' not found!") return;
}​

同样重要的是要注意,虽然这在 Firefox 和 Chrome 中有效,但“找不到字符串!”警告框不起作用

【问题讨论】:

    标签: javascript html internet-explorer


    【解决方案1】:

    这是改编自 another answer of mine 的版本。

    演示:http://jsfiddle.net/MRp2G/5/

    代码:

    function doSearch(text) {
        var sel;
        if (window.find && window.getSelection) {
            sel = window.getSelection();
            if (sel.rangeCount > 0) {
                sel.collapseToEnd();
            }
            window.find(text);
        } else if (document.selection && document.body.createTextRange) {
            sel = document.selection;
            var textRange;
            if (sel.type == "Text") {
                textRange = sel.createRange();
                textRange.collapse(false);
            } else {
                textRange = document.body.createTextRange();
                textRange.collapse(true);
            }
            if (textRange.findText(text)) {
                textRange.select();
            }
        }
    }
    

    【讨论】:

    • 哇!太感谢了!在我所有的搜索中,我找不到它!感谢您帮助我!
    • 所以现在我正在尝试制作一个带有选项的下拉列表,以便您可以在表格中搜索特定类别。我已经按 id 排序了所有列。知道如何去做吗?
    • @user1634292:哪一点让你绊倒了?
    • 好吧,我有下拉列表的简单 html 代码,我的表格被分类到 div 中,但我不知道如何将函数与 div 连接起来?我会使用什么方法?我尝试在文档调用后插入 getElementById() 但我不断收到错误消息。
    • @user1634292:演示页面可能会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2013-12-11
    相关资源
    最近更新 更多