成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

html如何實(shí)現(xiàn)查找框功能

html如何實(shí)現(xiàn)查找框功能?這個問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

密云網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),密云網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為密云上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的密云做網(wǎng)站的公司定做!

最近在搞一個被很多人改了的框架,天天看代碼看的頭的暈了,不過感覺進(jìn)步還挺大的,自己做了一個后臺可配置前臺查看兩個庫不同數(shù)據(jù)范圍的東西,還挺滿意,那天拿出來分享一下,今天先說一個這幾天做的功能,就是html頁面的查找功能。

這個功能主要是實(shí)現(xiàn)在查找框內(nèi)輸入字符,之后按后面的上一個下一個按鈕,會自動把查詢區(qū)域內(nèi)的匹配字符用特殊的樣式標(biāo)記,之后可以繼續(xù)按上一個下一個按鈕把按照順序瀏覽匹配字符,并把當(dāng)前匹配的字符用另一種樣式與其他匹配字符加以區(qū)別。

前臺顯示大概是這個樣子:

html如何實(shí)現(xiàn)查找框功能

html是這樣:

 <p class="container" style="z-index: 999" id="searchp">
        <p class="keyword-search">
            查找:
            <input id="key" type="text" style="width: 200px;" placeholder="關(guān)鍵詞" />
            <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>
        </p>
    </p>

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>

接下來記一下實(shí)現(xiàn)原理:

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

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

focusMove方法就是使頁面定位到當(dāng)前元素的操作。

學(xué)到的jquery方法:

eq() 選擇器:選擇器選取帶有指定 index 值的元素。例如:$(".contrast .result:eq(1)"),就是選擇類名contrast元素中的第二個類名為result的元素。

parents()方法:元素的所有父元素。$("p").parents('.contrast-wrap'),p元素所有類名為contrast-wrap的元素。

replace()方法:用指定的html內(nèi)容替換被選元素,注意是把被選元素的元素也替換掉。

offset()方法:返回或設(shè)置匹配元素相對于文檔的偏移(位置)。

scrollTop()方法:返回或設(shè)置匹配元素的滾動條的垂直位置。

感謝各位的閱讀!看完上述內(nèi)容,你們對html如何實(shí)現(xiàn)查找框功能大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前標(biāo)題:html如何實(shí)現(xiàn)查找框功能
當(dāng)前路徑:http://jinyejixie.com/article6/pgsjog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站內(nèi)鏈、網(wǎng)站收錄域名注冊、自適應(yīng)網(wǎng)站面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)
普洱| 永善县| 武邑县| 额敏县| 墨脱县| 建瓯市| 晴隆县| 乌拉特后旗| 万山特区| 大城县| 桦南县| 梁山县| 大余县| 西畴县| 淄博市| 沧州市| 通海县| 西充县| 湘乡市| 芮城县| 天全县| 江山市| 合阳县| 临海市| 三原县| 周宁县| 大理市| 闻喜县| 济阳县| 江孜县| 林甸县| 灵宝市| 兴文县| 同心县| 昭通市| 洛浦县| 青河县| 巧家县| 保山市| 宝兴县| 广东省|