老师有问题

来源:3-2 改写搜索框功能--自动完成

CC陈十一

2019-04-22 22:23:33

(function($){
	'use strict';

	function Search($elem,options){
		this.$elem=$elem;
		this.$options=options;

		this.$form=this.$elem.find('.form-search');
		this.$input=this.$elem.find('.search-inputbox');
		this.$layer=this.$elem.find('.search-layer');
		this.$elem.on('click','.search-btn',$.proxy(this.submit,this));
		if(this.options.autocomplete){
		
		
		//截止这次视频3分钟之前我都是跟着老师写的,但是会报错,说options.autocomplete是未定义的,后来我把它改成
		//options.autocomplete或是this.$options.autocomplete就不会报错,这是为什么
			this.autocomplete();
		}
	};
	Search.DEFAULTS={
		autocomplete:false,
		url:'https://suggest.taobao.com/sug?code=utf-8&_ksTS=1555933233907_926&callback=jsonp927&k=1&area=c2c&bucketid=10&q=',
		css3:false,
		js:false,
		animation:'fade'
	};
	Search.prototype.submit=function(){
		if($.trim(this.$input.val())===''){
			return false;
		}
		this.$form.sumbit();
	};
	Search.prototype.autocomplete=function(){
		this.$input.on('input',$.proxy(this.getData,this));
	};
	Search.prototype.getData=function(){
 		var self=this;

		$.ajax({
			url:this.options.url+this.$input.val(),
			dataType:'jsonp'
		}).done(function(data){
			self.$elem.trigger('search-getData',[data]);
		}).fail(function(){
			self.$elem.trigger('search-noData');
		});
	};
	Search.prototype.showLayer=function(){

	};
	Search.prototype.hidelayer=function(){

	};


	 $.fn.extend({
        search: function(option) {
            return this.each(function() {
                
                var $this=$(this),
                search=$this.data('search'),
                options = $.extend({}, Search.DEFAULTS, $(this).data(), typeof option==='object'&&option);
                // search(this, options);  
                if(!search){//解决多次调用search问题
                    $this.data('search',search=new Search($this,options));

                }  

                if(typeof search[option]==='function'){
                    search[option]();

                }

            });

        }
    });
})(jQuery);


写回答

2回答

好帮手慕星星

2019-04-23

同学你好,意思是这个条件会报错是吗?

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

而视频中老师用的是options:

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

所以会有问题,不是sublime版本的问题。

祝学习愉快!

0

CC陈十一

提问者

2019-04-22

是不是因为sublime版本的问题

0

0 学习 · 14456 问题

查看课程