// javascript document //友情连接下啦 function diy_select() { this.init.apply(this, arguments) }; diy_select.prototype = { init: function(opt) { this.setopts(opt); this.o = this.getbyclass(this.opt.ttcontainer, document, 'div'); //容器 this.b = this.getbyclass(this.opt.ttdiy_select_btn); //按钮 this.t = this.getbyclass(this.opt.ttdiy_select_txt); //显示 this.l = this.getbyclass(this.opt.ttdiv_select_list); //列表容器 this.ipt = this.getbyclass(this.opt.ttdiy_select_input); //列表容器 this.lengths = this.o.length; this.showselect(); }, addclass: function(o, s) //添加class { o.classname = o.classname ? o.classname + ' ' + s : s; }, removeclass: function(o, st) //删除class { var reg = new regexp('\\b' + st + '\\b'); o.classname = o.classname ? o.classname.replace(reg, '') : ''; }, addevent: function(o, t, fn) //注册事件 { return o.addeventlistener ? o.addeventlistener(t, fn, false) : o.attachevent('on' + t, fn); }, showselect: function() //显示下拉框列表 { var this = this; var inow = 0; this.addevent(document, 'click', function() { for(var i = 0; i < this.lengths; i++) { this.l[i].style.display = 'none'; } }) for(var i = 0; i < this.lengths; i++) { this.l[i].index = this.b[i].index = this.t[i].index = i; this.t[i].onclick = this.b[i].onclick = function(ev) { var e = window.event || ev; var index = this.index; this.item = this.l[index].getelementsbytagname('li'); this.l[index].style.display = this.l[index].style.display == 'block' ? 'none' : 'block'; for(var j = 0; j < this.lengths; j++) { if(j != index) { this.l[j].style.display = 'none'; } } this.addclick(this.item); e.stoppropagation ? e.stoppropagation() : (e.cancelbubble = true); //阻止冒泡 } } }, addclick: function(o) //点击回调函数 { if(o.length > 0) { var this = this; for(var i = 0; i < o.length; i++) { o[i].onmouseover = function() { this.addclass(this, this.opt.ttfcous); } o[i].onmouseout = function() { this.removeclass(this, this.opt.ttfcous); } o[i].onclick = function() { var index = this.parentnode.index; //获得列表 this.t[index].innerhtml = this.ipt[index].value = this.innerhtml.replace(/^\s+/, '').replace(/\s+&/, ''); this.l[index].style.display = 'none'; } } } }, getbyclass: function(s, p, t) //使用class获取元素 { var reg = new regexp('\\b' + s + '\\b'); var aresult = []; var aelement = (p || document).getelementsbytagname(t || '*'); for(var i = 0; i < aelement.length; i++) { if(reg.test(aelement[i].classname)) { aresult.push(aelement[i]) } } return aresult; }, setopts: function(opt) //以下参数可以不设置 //设置参数 { this.opt = { ttcontainer: 'diy_select', //控件的class ttdiy_select_input: 'diy_select_input', //用于提交表单的class ttdiy_select_txt: 'diy_select_txt', //diy_select用于显示当前选中内容的容器class ttdiy_select_btn: 'diy_select_btn', //diy_select的打开按钮 ttdiv_select_list: 'diy_select_list', //要显示的下拉框内容列表class ttfcous: 'focus' //得到焦点时的class } for(var a in opt) //赋值 ,请保持正确,没有准确判断的 { this.opt[a] = opt[a] ? opt[a] : this.opt[a]; } } } var ttdiy_select = new diy_select({ //参数可选 ttcontainer: 'diy_select', //控件的class ttdiy_select_input: 'diy_select_input', //用于提交表单的class ttdiy_select_txt: 'diy_select_txt', //diy_select用于显示当前选中内容的容器class ttdiy_select_btn: 'diy_select_btn', //diy_select的打开按钮 ttdiv_select_list: 'diy_select_list', //要显示的下拉框内容列表class ttfcous: 'focus' //得到焦点时的class }); //如同时使用多个时请保持各class一致.