function mdiv (){
this.root;//选择框
this.selroot;//选择框选中后更改的区域
this.seldiv;//浮动层最外层
this.cnt;//选项最外层,所有选项都要添加到这里
this.length = 0;//选项数量;
this.value;//当前选项值
this.items = [];//所有选项
this.selectIndex = 0;//当前选项序号
this.onchange = function (){};//选择事件函数
}
mdiv.activeDiv = null;
mdiv.curRoot = null;
mdiv.prototype.isIE=function (){
return (navigator.userAgent.toLowerCase().indexOf("ie")  > -1);
}
mdiv.prototype.getOffset=function (element){//计算对象在页面上的位置
  var valueT = 0, valueL = 0; 
  do {valueT += element.offsetTop || 0; 
valueL += element.offsetLeft || 0; 
element = element.offsetParent; }
while (element); 
  return [valueL, valueT];
}
mdiv.prototype.init=function (root,selroot) {
alert(root);
var me = this;
me.root = root;
me.selroot = selroot || root;
me.seldiv = mdiv.Create("div",false,"mdiv");
me.cnt = mdiv.Create("div",me.seldiv);
me.seldiv.style.display = "none";
try{
document.attachEvent("onmousedown",function(){me.disdiv();});
}
catch (ex){
document.addEventListener("mousedown",function(){me.disdiv();}, false);
}

me.root.onclick = function (){
me.show();
me.HideSome();
me.autoHeight();
}

}
mdiv.prototype.select=function (obj) {
var me = this;
var value = obj.getAttribute("sel_value");
me.value = value;
me.selectIndex = obj.getAttribute("sel_index");
me.selroot.style.padding = 0;
me.selroot.innerHTML = obj.innerHTML;
me.onchange();
me.hide();
}
mdiv.prototype.show=function () {
var me = this;
var pos = me.getOffset(me.root);
var seldiv = me.seldiv;
if(seldiv.style.display != "none"){
seldiv.style.display = "none";
return;
}
var ss=seldiv.style;
ss.background="#FFF";
ss.border="solid 1px #999";
ss.borderTop="none";
ss.position = "absolute";
ss.left = pos[0] + "px";
ss.top= pos[1] + me.root.offsetHeight-1+"px";
ss.width = (me.root.offsetWidth-((document.compatMode == "BackCompat")?0:2) )+ "px";
ss.display = "";
}
mdiv.prototype.hide=function () {
var me = this;
me.seldiv.style.display = "none";
}
mdiv.prototype.disdiv=function () {
var me = this;
//alert(me.seldiv.style.display);
if(me.seldiv.style.display != "none"){
me.hide();
}
me.ShowSome();
}
mdiv.prototype.add=function (obj,cssHover,value,selected) {
var me=this;
me.items.push(obj);
obj.setAttribute("sel_value",value);
try{
me.cnt.appendChild(obj);
me.length = me.items.length;
var index = me.items.length - 1;
obj.setAttribute("sel_index",index);
obj.onmouseover=function(){
obj.setAttribute("cssBack",obj.className);
obj.className = obj.className + cssHover;
}
obj.onmouseout = function(){
obj.className = obj.className.replace(cssHover,"");
}
obj.onmousedown = function(){
me.select(obj);
}
}
catch(ex){}
if(selected){
me.select(obj);
}
}
mdiv.Create=function (str,parentObj,cssName){
var obj=document.createElement(str);
if(parentObj)
{parentObj.appendChild(obj);}
else
{document.body.appendChild(obj);}
if(cssName)obj.className=cssName;
return obj;
}//简化
mdiv.prototype.autoHeight=function(){};
mdiv.prototype.HideSome=function(){};
mdiv.prototype.ShowSome=function(){};哪位能帮解释一下

解决方案 »

  1.   

    function mdiv (){ //创建mdiv对象
    this.root;//选择框 
    this.selroot;//选择框选中后更改的区域 
    this.seldiv;//浮动层最外层 
    this.cnt;//选项最外层,所有选项都要添加到这里 
    this.length = 0;//选项数量; 
    this.value;//当前选项值 
    this.items = [];//所有选项 
    this.selectIndex = 0;//当前选项序号 
    this.onchange = function (){};//选择事件函数 

    mdiv.activeDiv = null; 
    mdiv.curRoot = null; 
    mdiv.prototype.isIE=function (){ //判断是否是IE
    return (navigator.userAgent.toLowerCase().indexOf("ie")  > -1); 

    mdiv.prototype.getOffset=function (element){//计算对象在页面上的位置 ,为什么要这样做
      var valueT = 0, valueL = 0; 
      do {valueT += element.offsetTop || 0; 
    valueL += element.offsetLeft || 0; 
    element = element.offsetParent; } 
    while (element); 
      return [valueL, valueT]; 

    mdiv.prototype.init=function (root,selroot) { //初始化select 
    alert(root); 
    var me = this; 
    me.root = root; 
    me.selroot = selroot || root; 
    me.seldiv = mdiv.Create("div",false,"mdiv"); 
    me.cnt = mdiv.Create("div",me.seldiv); 
    me.seldiv.style.display = "none"; 
    try{ 
    document.attachEvent("onmousedown",function(){me.disdiv();}); //注册事件,IE5.0以下好像不支持,所以就catch了

    catch (ex){ 
    document.addEventListener("mousedown",function(){me.disdiv();}, false); 
    } me.root.onclick = function (){ //给模拟的select注册onclick事件?定位点相当于那难看的图片?
    me.show(); 
    me.HideSome(); 
    me.autoHeight(); 
    } } 
    mdiv.prototype.select=function (obj) { //给当前的div对象赋值???
    var me = this; 
    var value = obj.getAttribute("sel_value"); 
    me.value = value; 
    me.selectIndex = obj.getAttribute("sel_index"); 
    me.selroot.style.padding = 0; 
    me.selroot.innerHTML = obj.innerHTML; 
    me.onchange(); 
    me.hide(); 

    mdiv.prototype.show=function () { 
    var me = this; 
    var pos = me.getOffset(me.root); 
    var seldiv = me.seldiv; 
    if(seldiv.style.display != "none"){ 
    seldiv.style.display = "none"; 
    return; 

    var ss=seldiv.style; 
    ss.background="#FFF"; 
    ss.border="solid 1px #999"; 
    ss.borderTop="none"; 
    ss.position = "absolute"; 
    ss.left = pos[0] + "px"; 
    ss.top= pos[1] + me.root.offsetHeight-1+"px"; 
    ss.width = (me.root.offsetWidth-((document.compatMode == "BackCompat")?0:2) )+ "px"; 
    ss.display = ""; 

    mdiv.prototype.hide=function () { 
    var me = this; 
    me.seldiv.style.display = "none"; 

    mdiv.prototype.disdiv=function () { 
    var me = this; 
    //alert(me.seldiv.style.display); 
    if(me.seldiv.style.display != "none"){ 
    me.hide(); 

    me.ShowSome(); 

    mdiv.prototype.add=function (obj,cssHover,value,selected) { 
    var me=this; 
    me.items.push(obj); 
    obj.setAttribute("sel_value",value); 
    try{ 
    me.cnt.appendChild(obj); 
    me.length = me.items.length; 
    var index = me.items.length - 1; 
    obj.setAttribute("sel_index",index); 
    obj.onmouseover=function(){ 
    obj.setAttribute("cssBack",obj.className); 
    obj.className = obj.className + cssHover; 

    obj.onmouseout = function(){ 
    obj.className = obj.className.replace(cssHover,""); 

    obj.onmousedown = function(){ 
    me.select(obj); 


    catch(ex){} 
    if(selected){ 
    me.select(obj); 


    mdiv.Create=function (str,parentObj,cssName){ 
    var obj=document.createElement(str); 
    if(parentObj) 
    {parentObj.appendChild(obj);} 
    else 
    {document.body.appendChild(obj);} 
    if(cssName)obj.className=cssName; 
    return obj; 
    }//简化 
    mdiv.prototype.autoHeight=function(){}; 
    mdiv.prototype.HideSome=function(){}; 
    mdiv.prototype.ShowSome=function(){}; 
    咦这个的整体思路我不能连贯起来,哪位指点一下谢谢