六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

html頁面用js中完成搜索技巧

[摘要]今天先說一個這幾天做的功能,就是html頁面的查找功能。 這個功能主要是實現在html查找框內輸入字符,之后按后面的上一個下一個按鈕,會自動把查詢區域內的匹配字符用特殊的樣式標記,之后可以繼續按上一個下一個按鈕把按照順序瀏覽匹配字符,并把當前匹配的字符用另一種樣式與其他匹配字符加以區別。付有htm...
今天先說一個這幾天做的功能,就是html頁面的查找功能。 這個功能主要是實現在html查找框內輸入字符,之后按后面的上一個下一個按鈕,會自動把查詢區域內的匹配字符用特殊的樣式標記,之后可以繼續按上一個下一個按鈕把按照順序瀏覽匹配字符,并把當前匹配的字符用另一種樣式與其他匹配字符加以區別。付有html代碼哦!

樣式演示:

@WG(NDK33XF7CN7R~]85ZI5.png

代碼演示:

html

<div class="container" style="z-index: 999" id="searchDiv">
<div class="keyword-search">
查找:
<input id="key" type="text" style="width: 200px;" placeholder="關鍵詞" />
<a href="javascript:void(0);" class="prev" onclick='wordSearch(1)'><i class="c-icon"></i></a>
<a href="javascript:void(0);" class="next" onclick='wordSearch()'><i class="c-icon"></i></a>
</div>
</div>

script

    <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='result" + (index++) + "' class='result'>" + key + "</span>")); // 替換
                    });
                } else {
                    $(".contrast-wrap").addClass('current');
                    currentLength = $(".current").length;
                    $(".contrast").each(function () {
                        $(this).html($(this).html().replace(new RegExp(key, "gm"), "<span id='result" + (index++) + "' class='result'>" + key + "</span>")); // 替換
                    });
                }
                //$("#key").val(key);
                oldKey0 = key;

                //$(".contrast .result").each(function () {
                //    $(this).parents('.contrast-wrap').addClass('current');
                //    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('.contrast-wrap').addClass('current');
            $(".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);
            }

        }


        $('#key').change(function () {
            if ($('#key').val() == "") {
                index0 = 0;
                $(".contrast .result").each(function () {
                    $(this).replaceWith($(this).html());
                });
                oldKey0 = "";
            }
        });
    </script>

接下來記一下實現原理:

首先先把上一次的查詢結果清除掉,然后根據key的值,用正則表達式把區域內所有匹配的字符全都加上特殊的樣式,比如方法中就全部加了一個類名為result的span標簽,用odKey0變量記錄key的值(下次再進入先比較如果一樣的話說明是要看下一個或者上一個的內容,就不用在進入用正則表達式匹配了),oldCount0記錄總共查詢出來的個數,newflag置0(如果不是初次查詢newflag為1)。

接著進入getNext方法,flg表示用戶按下的是上一個還是下一個按鈕,用index0記錄當前查看的是哪一個匹配字符,與oldCount0比較,確定是遞增或遞減還是置0(如果大于oldCount0或者小于0,就要重新開始)。

focusMove方法就是使頁面定位到當前元素的操作。

相關推薦:

HTML怎么實現數字焦點圖輪播代碼

html里怎么插入圖片

HTML里DIV相互重疊怎么辦

HTML里怎么使用margin 0 auto

以上就是html頁面用js中實現查找功能的詳細內容,更多請關注php中文網其它相關文章!


網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。




主站蜘蛛池模板: 日韩国产成人精品视频 | 亚洲性免费 | 色www视频永久免费 色www免费视频 | 特黄一级大片 | 特级黄色视频毛片 | 亚洲最大成人综合网 | 羞羞人成午夜爽爽影院 | 天天躁日日躁狠狠躁视频下载播二 | 最新69国产成人精品视频69 | 色无极影院亚洲 | 天天干天天干天天色 | 中文字幕视频在线观看 | 天天操狠狠操夜夜操 | 亚洲成综合人影院在院播放 | 天天操天天操天天干 | 中文字幕网资源站永久资源 | 日韩精品国产自在欧美 | 亚洲va久久久噜噜噜久久天堂 | 日本在线视 | 亚洲视频在线一区 | 天天射综合网站 | 亚洲美女综合 | 最新理论片 | 日韩一区二三区国产好的精华液 | 日韩精品亚洲专区在线影视 | 又大又粗又长又硬好爽国产 | 永久看一二三四线 | 午夜骚片 | 四虎成人免费影院网址 | 在线观看视频国产 | 欲色啪 | 亚洲国产第一 | 四虎影视免费永久在线观看黄 | 亚洲精品男人天堂 | 在线看污视频 | 性荡视频播放器在线视频播放 | 欧美午夜视频 | 欧美一级大黄 | 日本高清不卡在线 | 日本色资源 | 日韩 欧美 亚洲 |