按照视频中老师的方式写代码没有任何效果

来源:3-3 代码优化

慕斯_Irice368

2019-07-26 21:24:14

<header>

    <div class="header-container">

        <div class="header-logo fl">

        <img src="img/header-logo.png" />

        <h1><a href="#">慕多多</a></h1>

        </div>

        <div id="header-search" class="search fl">

            <form action="//ai.taobao.com/desire/index.html"class="search-form">

                <input type="text" class="search-inputbox fl" name="q" autocomplete="off" placeholder="灵魂美食一元抢" />

                <input type="submit" class="submit-btn fl" value="搜索" />

                <ul class="search-layer">

                    <!--<li class="search-layer-item text-ellipsis">111</li>

                    <li class="search-layer-item text-ellipsis">111</li>

                    <li class="search-layer-item text-ellipsis">111</li>-->

                </ul>

            </form>

        </div>

        <div class="fr"></div>

    </div>

</header>    

/*header区*/

header{

    width:100%;

    height:124px;

    font:14px "微软雅黑";

}

.header-container{

    width:1200px;

    height:100%;

    margin:0 auto;

    background:#f3f5f7;

}

.header-logo,.header-logo img {

    width:132px;

    height:42px;    

}

.header-logo{

   margin:36px 146px 0 15px;

}

.header-logo a {

   display: inline-block;

   width: 100%;

   height: 47px;

   text-indent:-9999px;

   overflow:hidden;   

 }

.search{

    width:678px;

    height:40px;

    border:1px solid #cfd2d5;

    margin-top:36px;

}

.search-inputbox{

   width:605px;

   height:40px;

   padding:0 10px;

   border:none;

   outline:none;

}

.submit-btn{

   width:73px;

   height:40px;

   border:none;

   color:white;

   background:#ca003f;

}


.search-layer{

    position:relative;

    top:0px;

    left:-1px;

    width:100%;     

    background:white;

    border:1px solid #cfd2d5;

    clear:both;

    display:none;

}

.search-layer-item {   

    height:30px;

    line-height:30px;

    padding:0 10px; 

}

.search-layer-item:hover {

    background:#f3f5f7;

}

.text-ellipsis{

    text-overflow:ellipsis;

    white-space:nowrap;

    overflow:hidden;

}


(function ($) {

    "use strict";

  

    function Search($elem, options) {

        this.$elem = $elem;

        this.options = options;

        this.$input = this.$elem.find(".search-inputbox"),

        this.$form = this.$elem.find(".search-form"),

        this.$layer = this.$elem.find(".search-layer");


        this.$elem.on("click", ".search-btn", $.proxy(this.submit, this));

        if (this.options.autocomplete) {

            this.autocomplete();

        }


    }

    Search.DEFAULS = {

        autocomplete:false,

        url: 'https://suggest.taobao.com/sug?code=utf-8&&_ksTS=1564047707915_833&callback=jsonp834&k=1&area=c2c&bucketid=5&q=',

        css3: false,

        js: false,

        animation:"fade"

    }

    Search.prototype.submit = function () {

        if (getInputVal() === "") {

                      return false;

        }

        this.$form.submit();

    }

    Search.prototype.autocomplete = function () {

        this.$input

            .on("input", $.proxy(this.getData, this))

            .on("focus", $.proxy(this.showLayer, this))

            .on("click", function () {

               return false;

             });

        this.$layer.showHide(this.options);   

        $(document).on("click",$.proxy(this.hideLayer, this))

    }

    Search.prototype.getData = function () {

        var self = this;

        $.ajax({

            url: this.otptions.url + this.getInputVal(),

            dataType: "jsonp"

           }).done(function (data) {          

               sele.$elem.trgger("search-getData",[data,self.$layer]);

           }).fail(function () {

               sele.$elem.trgger("search-noData");

           })

        }

    Search.prototype.showLayer = function () {

        if (this.$layer.children().length === 0) return;

        this.$layer.showHide("show")

    }

    Search.prototype.hideLayer = function () {

        this.$layer.showHide("hide")

    }

    Search.prototype.getInputVal = function () {

      return  $.trim(this.$input.val())

    }

    Search.prototype.setInputVal = function (val) {

        this.$input.val(removeHtmlTags(val));

        function removeHtmlTags(str) {

        return str.replace(/<(?:[^>'"]|"[^"]*"|'[^']*')*>/g,'');

           }

    }

    $.fn.extend({

        search: function (option,value) {

            return this.each(function () {

                var $this = $(this),

                    search = $this.data("search"),

                    options = $.extend({}, Search.DEFAULS, $this.data(), typeof option === 'object' && option);

                if (!search) {

                    $this.data('search', search = new Search($this, options));

                }

                if (typeof search[option] === "function") {

                    search[option](value);

                }

            })

        }

    })


    var $headerSearch = $("#header-search"), 

         html = "",

         maxNum = 10;

    $headerSearch.on("search-getData", function (e, data,$layer) {

        $this = $(this);

        html = creatHeaderSearchLayer(data, maxNum);

        $layer.html(html);

        if (html) {

            $this.search("showLayer");

        } else {

            $this.search("hideLayer");

        }


        $headerSearch.search({

            autocomplete: true,

            css3: false,

            js: false,

            animation: "fade"

        })

       

         $layer.on("click",".search-layer-item",function () {

         $input.val($(this).html());

         $form.submit();

           });

 

        function creatHeaderSearchLayer(data, maxNum) {

            var html = "",

                dataNum = data["result"].length;

            if (dataNum === 0) {

                return '';

            }

            for (var i = 0; i < dataNum; i++) {

                if (i >= maxNum) break;

                html += " <li class='search-layer-item text-ellipsis'>" + data["result"][i][0] + "</li>";

            }

            return html;

        }

    }). on("search-noData", function (e,$layer) {

        $(this).search("hideLayer");

        $layer.html("");

        }).on("click", ".search-layer-item", function () {

            $headerSearch.search("setInputVal",$this.html());

            $headerSearch.search("submit");

        });    

    })(jQuery)

连下拉层都没显示出来,更不知道如何按照老师下节课的方式来优化了

this.$layer.showHide("show") 另外showHide在老师视频讲的时候我找不到哪里来的

写回答

3回答

樱桃小胖子

2019-07-27

http://img.mukewang.com/climg/5d3c34b80001a2e910710885.jpg

建议将

http://img.mukewang.com/climg/5d3c34e30001f55c10250651.jpg

http://img.mukewang.com/climg/5d3c34ee0001bda910530866.jpg

这一块的内容放在search.js文件中哦

测试效果如下:

http://img.mukewang.com/climg/5d3c351d0001370319060956.jpg

希望可以帮到你!

0

慕斯_Irice368

提问者

2019-07-27

老师我想说的是搜索框输入内容时下拉层自动显示类似匹配信息 通过ajax来获取 这节课不就是讲的这个内容吗

视频中老师使用组件化的方式来编写代码  我跟着老师的视屏一步步来做的没有效果

另外来时说的HTML代码我注释了  在jquery代码中使用.html的方法来填充了(视频中也是)   老师是不是没仔细看代码

http://img.mukewang.com/climg/5d3c15c00001476310500278.jpg

0

樱桃小胖子

2019-07-27

同学具体是想实现哪里的效果呢?老师看到你这边给出的代码,缺少导航下拉菜单的html代码呀

http://img.mukewang.com/climg/5d3bbc290001dd5f12970884.jpg

建议同学再详细描述一下问题或者是参考一下课程源码,如有疑问可以再次提问,另外建议作业尽量脱离老师的视频,自己独立去梳理思路来实现哦,批作业的老师也会给出非常详细的修改和完善建议的

祝学习愉快!

0

0 学习 · 14456 问题

查看课程