Home > Web Front-end > JS Tutorial > body text

js implements select drop-down box menu_javascript skills

WBOY
Release: 2016-05-16 15:27:14
Original
1689 people have browsed it

The example in this article describes the detailed code for implementing the select drop-down menu in js. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:

The specific code is as follows:


<html>

<head>
  <title></title>
  <style type="text/css">
   #gridComboBox {
    background: #fff;
    border: 1px solid #2d78f4;
    border-radius: 2px;
    -moz-box-shadow: inset 0 0 4px #06c;
    -webkit-box-shadow: inset 0 0 4px #06c;
    box-shadow: inset 0 0 4px #06c;
  }
  </style>
</head>

<body>
  <input onclick="doClick(this,'ddd(aa/bb/cc/erererer/dfdfdfdfdfdfdfdfdfdfdfdfdf/ejjejrjerjer//erererer ////////)eee')" />
  <input onclick="doClick(this,'ddd(aa/bb/cc/erererer/dfdfdfdfdfdfdfdfdfdfdfdfdf/ejjejrjerjer//erererer ////////)eee')" style='width: 300px;' />
  <p><b>Needed Properties:</b> sender.offsetTop & sender.offsetLeft & sender.offsetHeight</p>
</body>

</html>
<script type="text/javascript">

function delGridComboBox() { // 删除弹出框
  var divContainer = document.getElementById('gridComboBox');
  if (divContainer) {
    divContainer.parentNode.removeChild(divContainer);
  }
}

function doClick(sender, str) {//str='ddd(aa/bb/cc/erererer/dfdfdfdfdfdfdfdfdfdfdfdfdf/ejjejrjerjer//erererer ////////)eee' 只取()得内容/分割做为数据源

  delGridComboBox();

  // console.log(sender);
  // for(var i in sender) 
  // {
  //   console.log(i+"|"+sender[i]);
  // }

  
  var re = /[^\)\(]*/g //取出()中的内容作为下拉数据源
  var fit = str.match(re);
  var fmt = fit[2].split('/');

  var divContainer = document.createElement('div');
  divContainer.style.width = sender.clientWidth + 2 + "px";
  divContainer.style.overflow = "hidden";
  divContainer.style.position = 'absolute'; 
  divContainer.style.top = sender.offsetTop + sender.offsetHeight + 'px';
  divContainer.style.left = sender.offsetLeft + 'px';
  divContainer.style.zIndex = 999;
  divContainer.id = "gridComboBox";

  for (var i = 0; i < fmt.length; i++) {   

    console.log('fmt[i]>>', fmt[i]);
    var txt = document.createElement('div'); 
    txt.innerHTML = fmt[i];
    txt.title = txt.innerHTML;
    txt.style.margin = 3+'px';    
    txt.addEventListener('mouseover', function changeBg(event) {
      event.target.style.fontWeight = 'bold';
      event.target.style.color = 'white';
      event.target.style.backgroundColor = '#2d78f4';
    }, false);
    txt.addEventListener('mouseout', function unChangeBg(event) {
      event.target.style.fontWeight = 'normal';
      event.target.style.color = 'black';
      event.target.style.backgroundColor = 'white';
    }, false);
    txt.onclick = function(subSender) {
      sender.value = subSender.target.innerText;
      delGridComboBox();
    };
    divContainer.appendChild(txt);
  };
  
  document.body.appendChild(divContainer);
};
</script>
Copy after login

The above is the code for implementing the select drop-down menu in js. I hope this article will be helpful to everyone in learning javascript programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template