html search box function

墨辰丷
Release: 2018-06-04 14:28:06
Original
4065 people have browsed it

The search function of the html page is mainly used to enter characters in the search box. Let me share with you the search function of the html page through this article. Friends who need it can refer to it.

Recently I am working on a project that has been modified by many people. framework, I feel dizzy when I look at the code every day, but I feel that I have made a lot of progress. I made a backend and configurable frontend to view the different data ranges of the two libraries. I am quite satisfied. I took it out to share it that day. Today I will talk about a function that I have been working on in the past few days, which is the search function of html pages.

This function is mainly used to enter characters in the search box, and then press the previous and next buttons. The matching characters in the query area will be automatically marked with a special style. You can continue to press the previous and next buttons. A button will browse the matching characters in order and use another style to distinguish the current matching character from other matching characters.

The front desk display probably looks like this:

html looks like this:

 <p class="container" style="z-index: 999" id="searchp">
        <p class="keyword-search">
            查找:
            <input id="key" type="text" style="width: 200px;" placeholder="关键词" />
            <a href="javascript:void(0);" class="prev" onclick=&#39;wordSearch(1)&#39;><i class="c-icon"></i></a>
            <a href="javascript:void(0);" class="next" onclick=&#39;wordSearch()&#39;><i class="c-icon"></i></a>
        </p>
    </p>
Copy after login

script code:

  <script>//搜索功能
        var oldKey0 = "";
        var index0 = -1;var oldCount0 = 0;
        var newflag = 0;
        var currentLength = 0;
        function wordSearch(flg) {
            var key = $("#key").val(); //取key值
            if (!key) {
                return; //key为空则退出
            }
            getArray();
            focusNext(flg);
        }
        function focusNext(flg) {
            if (newflag == 0) {//如果新搜索,index清零
                index0 = 0;
            }
            if (!flg) {
                if (oldCount0 != 0) {//如果还有搜索
                    if (index0 < oldCount0) {//左边如果没走完,走左边
                        focusMove(index0);
                        index0++;
                    } else if (index0 == oldCount0) {//都走完了
                        index0 = 0;
                        focusMove(index0);
                        index0++;
                    }
                    else {
                        index0 = 0;//没确定
                        focusMove(index0);
                        index0++;
                    }
                }
            } else {
                if (oldCount0 != 0) {//如果还有搜索
                    if (index0 <= oldCount0 && index0 > 0) {//左边如果没走完,走左边
                        index0--;
                        focusMove(index0);
                    } else if (index0 == 0) {//都走完了
                        index0 = oldCount0;
                        index0--
                        focusMove(index0);
                    }
                }
            }
        }
        function getArray() {
            newflag = 1;
            $(".contrast .result").removeClass("res");
            var key = $("#key").val(); //取key值
            if (!key) {
                oldKey0 = "";
                return; //key为空则退出
            }
            if (oldKey0 != key || $(".current").length != currentLength) {
                //重置
                index0 = 0;
                var index = 0;
                $(".contrast .result").each(function () {
                    $(this).replaceWith($(this).html());
                });
                pos0 = new Array();
                if ($(".contrast-wrap").hasClass("current")) {
                    currentLength = $(".current").length;
                    $(".current .contrast").each(function () {
                        $(this).html($(this).html().replace(new RegExp(key, "gm"), "<span id=&#39;result" + (index++) + "&#39; class=&#39;result&#39;>" + key + "</span>")); // 替换
                    });
                } else {
                    $(".contrast-wrap").addClass(&#39;current&#39;);
                    currentLength = $(".current").length;
                    $(".contrast").each(function () {
                        $(this).html($(this).html().replace(new RegExp(key, "gm"), "<span id=&#39;result" + (index++) + "&#39; class=&#39;result&#39;>" + key + "</span>")); // 替换
                    });
                }
                //$("#key").val(key);
                oldKey0 = key;
                //$(".contrast .result").each(function () {
                //    $(this).parents(&#39;.contrast-wrap&#39;).addClass(&#39;current&#39;);
                //    pos0.push($(this).offset().top);
                //});
                // pos0.push($(".contrast .result:eq(2)").offset().top - $(".contrast .result:eq(2)").parents(".contrast").offset().top);
                oldCount0 = $(".contrast .result").length;
                newflag = 0;
            }
        }
        function focusMove(index0) {
            $(".contrast .result:eq(" + index0 + ")").parents(&#39;.contrast-wrap&#39;).addClass(&#39;current&#39;);
            $(".contrast .result:eq(" + index0 + ")").addClass("res");
            var top = $(".contrast .result:eq(" + index0 + ")").offset().top + $(".contrast .result:eq(" + index0 + ")").parents(".contrast").scrollTop();
            var intop = top - $(".contrast .result:eq(" + index0 + ")").parents(".contrast").offset().top;
            $(".contrast .result:eq(" + index0 + ")").parents(".contrast").animate({ scrollTop: intop }, 200);
            if ($(".contrast .result:eq(" + index0 + ")").parents(".contrast").scrollTop() == 0) {
                $("html, body").animate({ scrollTop: top - 200 }, 200);
            } else {
                $("html, body").animate({ scrollTop: $(".contrast .result:eq(" + index0 + ")").parents(".contrast").offset().top - 200 }, 200);
            }
        }
        $(&#39;#key&#39;).change(function () {
            if ($(&#39;#key&#39;).val() == "") {
                index0 = 0;
                $(".contrast .result").each(function () {
                    $(this).replaceWith($(this).html());
                });
                oldKey0 = "";
            }
        });
    </script>
Copy after login

Next, remember the implementation principle:

First clear the last query results, and then use regular expressions to match all matches in the area based on the key value. All the characters are added with a special style. For example, a span tag with the class name result is added to the method, and the odKey0 variable is used to record the value of the key (compare first when entering next time. If they are the same, it means that you need to look at the next or previous one. The content of one, there is no need to use regular expression matching when entering), oldCount0 records the total number of queries, and newflag is set to 0 (if it is not the first query, newflag is 1).

Then enter the getNext method. flg indicates whether the user pressed the previous or next button. Use index0 to record which matching character is currently viewed. Compare it with oldCount0 to determine whether it is incrementing or decrementing or setting to 0 ( If it is greater than oldCount0 or less than 0, it will start again).

The focusMove method is an operation that positions the page to the current element.

Jquery methods learned:

eq() Selector: The selector selects elements with a specified index value. For example: $(".contrast .result:eq(1)") selects the second element named result among the elements with class name contrast.

parents() method: all parent elements of the element. $("p").parents('.contrast-wrap'), all elements of the p element whose class name is contrast-wrap.

replace() method: Replace the selected element with the specified html content. Note that the elements of the selected element are also replaced.

offset() method: Returns or sets the offset (position) of the matching element relative to the document.

scrollTop() method: Returns or sets the vertical position of the scroll bar of the matching element.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Summary of HTML lists, forms, and semantic usage methods

Knowledge points for using postMessage in HTML5 Solve the cross-domain problem of POST in Ajax

##Chart.js lightweight HTML5 chart drawing tool library detailed steps to use

The above is the detailed content of html search box function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!