现成的例子和代码http://krolik.net/ComboBox.shtml

解决方案 »

  1.   

    可输入下拉框
    http://jkisjk.vip.sina.com/html/EditableSelect1.htm空间已经过期,代码无法更新,仅供效果演示
      

  2.   

    我试了一下,这样做是不是你要的呢?
    -----------------------------------------------------------------------------------
    <HTML>
    <HEAD>
    <TITLE> combo sample </TITLE>
    <script type="text/javascript">
    var _dom = (document.all ? (document.getElementById ? 2 : 1) :
        (document.getElementById ? 4 : (document.layers ? 3 : 0)));function setDivVisibility(div, visible) {
      if (_dom == 4 || _dom == 2 || _dom == 1) {
        div.style.visibility = (visible) ? 'inherit' : 'hidden';
        return;
      }
      if (_dom == 3) {
        div.visibility = (visible) ? 'inherit' : 'hide';
        return;
      }
    }
    function ComboBox(blurFunc) {
      this.type = "combo";
      this.blurFunc = blurFunc;
      this.visibility = false;  this.options  = new Array();
      this.expops   = new Array();
      this.value    = "";
      this.setFocus = setFocus_ComboBox;
      this.setVisibility = setVisibility_ComboBox;
      this.getVisibility = getVisibility_ComboBox;
      this.create();
    }function setFocus_ComboBox() {
      this.txtview.focus();
    }function setVisibility_ComboBox(visible) {
      this.visibility = visible;
      setDivVisibility(this.lyr, visible);
    }function getVisibility_ComboBox() {
      return this.visibility;
    }ComboBox.prototype.create = function() {
      this.lyr = document.createElement("DIV");
      this.lyr.style.position='absolute';  this.txtview = document.createElement("INPUT");
      this.txtview.name = "SPComboBox";
      this.txtview.id   = "SPComboBox";
      this.txtview.className = "combo-input";
      this.txtview.parent = this;
      this.txtview.onblur = blur_ComboBox;
      this.lyr.appendChild(this.txtview);
            
      this.valcon = document.createElement("INPUT");
      this.valcon.type = "hidden";
      this.lyr.appendChild(this.valcon);  this.button = document.createElement("BUTTON");
      this.button.appendChild(document.createTextNode('I'));
      this.button.className = "combo-button";
      this.button.style.position = "absolute";
      this.button.onblur = blur_ComboBox1;
      this.button.parent = this;  this.lyr.appendChild(this.button);
      this.button.onfocus = function () {
        //alert("[button.onfocus] start ... ");
        this.blur(); 
      };
      
      this.button.onmousedown = this.toggle;  this.txtview = this.lyr.childNodes[0];
      this.valcon  = this.lyr.childNodes[1];  document.body.appendChild(this.lyr);
    }ComboBox.prototype.toggle = function() {
      //alert("[toggle] start ... ");
    }function blur_ComboBox() {
      //alert("[blur_ComboBox] start ... ");
      var theCombo = this.parent;
      
      if (theCombo != null) {
        if (theCombo.blurFunc) {
          theCombo.blurFunc();
        }    theCombo.setVisibility(false);
      }
    }
    function blur_ComboBox1() {
      var theCombo = this.parent;
      
      if (theCombo != null) {
        if (theCombo.blurFunc) {
          theCombo.blurFunc();
        }    theCombo.setVisibility(true);
      }
    }
    function insertComboBox() {
      var combox = new ComboBox(onBlurFunc);
    }function onBlurFunc() {
      //alert("[onBlurFunc] start...");
    }</script></HEAD><BODY>
    <button onclick="insertComboBox();">Call insertComboBox()</button>
    </BODY>
    </HTML>