码迷,mamicode.com
首页 > Web开发 > 详细

纯JS搜索页面内容-我的扩展版本

时间:2014-05-13 22:31:43      阅读:540      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

我扩展了什么?

1.不区分大小写,且替换后保留原大小写内容.

2.自动滚动到所属位置.

时间比较赶 也比较粗糙.

原作 张鑫旭

 

-------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
$.tmp_textSearch_str = "";
$.fn.textSearch = function(str, options) { //纯JS字符串搜索
    var defaults = {
        divFlag: true,
        divStr: " ",
        markClass: "",
        markColor: "red",
        nullReport: true,
        scroll: 1,
        callback: function() {
            return false;
        }
    };
    var sets = $.extend({}, defaults, options || {}), clStr;
    if (sets.markClass) {
        clStr = "class=‘" + sets.markClass + "‘";
    } else {
        clStr = "style=‘color:" + sets.markColor + ";‘";
    }
 
    //对前一次高亮处理的文字还原    
    $("span[data-keyrole=‘mark‘]").each(function() {
        var text = document.createTextNode($(this).text());
        $(this).replaceWith($(text));
    });
 
 
    //字符串正则表达式关键字转化
    $.regTrim = function(s) {
        var imp = /[\^\.\\\|\(\)\*\+\-\$\[\]\?]/g;
        var imp_c = {};
        imp_c["^"] = "\\^";
        imp_c["."] = "\\.";
        imp_c["\\"] = "\\\\";
        imp_c["|"] = "\\|";
        imp_c["("] = "\\(";
        imp_c[")"] = "\\)";
        imp_c["*"] = "\\*";
        imp_c["+"] = "\\+";
        imp_c["-"] = "\\-";
        imp_c["$"] = "\$";
        imp_c["["] = "\\[";
        imp_c["]"] = "\\]";
        imp_c["?"] = "\\?";
        s = s.replace(imp, function(o) {
            return imp_c[o];
        });
        return s;
    };
    $(this).each(function() {
        var t = $(this);
        str = $.trim(str);
        if (str === "") {
            //alert("Keyword is empty.");
            return false;
        } else {
 
            //将关键字push到数组之中
            var arr = [];
            if (sets.divFlag) {
 
                arr = str.split(sets.divStr);
            } else {
                arr.push(str);
            }
        }
        var v_html = t.html();
        //删除注释
        v_html = v_html.replace(/<!--(?:.*)\-->/g, "");
 
        //过滤HTML片段
        var tags = /[^<>]+|<(\/?)([A-Za-z]+)([^<>]*)>/g;
        var a = v_html.match(tags), test = 0;
        $.each(a, function(i, c) {
            if (!/<(?:.|\s)*?>/.test(c)) {//非标签
                //开始执行替换
                $.each(arr, function(index, con) {
                    if (con === "") {
                        return;
                    }
                    var reg = new RegExp("(" + $.regTrim(con) + ")", "ig");
                    if (reg.test(c)) {
 
                        //正则替换
                        c = c.replace(reg, "#♂spstart# $1 #♂spend#");
                        test = 1;
                    }
                });
                c = c.replace(/#♂spstart#/g, "<span data-keyrole=‘mark‘ " + clStr + ">").replace(/#♂spend#/g, "</span>");
                a[i] = c;
            }
        });
        //将支离数组重新组成字符串
        var new_html = a.join("");
        $(this).html(new_html);
 
        if (test === 0 && sets.nullReport) {
            alert("No result found in this page.");
            return false;
        }
        sets.callback();
        if (sets.scroll) {
            //自动滚动
            $.tmp_textSearch_str = $.tmp_textSearch_str || str;
            if ($.tmp_textSearch_str == str) {
                $.tmp_textSearch_scollCount = $.tmp_textSearch_scollCount === undefined ? 0 : ($.tmp_textSearch_scollCount + 1);
            } else {
                $.tmp_textSearch_str = str;
                $.tmp_textSearch_scollCount = 0;
            }
            var span = $("span[data-keyrole=‘mark‘]")[$.tmp_textSearch_scollCount];
            if (!span) {
                $.tmp_textSearch_scollCount = 0;
                span = $("span[data-keyrole=‘mark‘]")[0];
            }
            var toTop = $(span).offset().top - 10;
            $(‘html,body‘).animate({scrollTop: toTop}, window.navigator.appName == ‘Opera‘ ? 0 : 100);
        }
 
    });
};

  

纯JS搜索页面内容-我的扩展版本,布布扣,bubuko.com

纯JS搜索页面内容-我的扩展版本

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/crabzzz/p/3724032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!