当前位置:Gxlcms > JavaScript > js实现select下拉框菜单

js实现select下拉框菜单

时间:2021-07-01 10:21:17 帮助过:7人阅读

本文实例讲述了js实现select下拉框菜单的详细代码。分享给大家供大家参考。具体如下:
运行效果截图如下:

具体代码如下:

<!DOCTYPE html>

  1. <html>
  2. <head>
  3. <title></title>
  4. <style type="text/css">
  5. #gridComboBox {
  6. background: #fff;
  7. border: 1px solid #2d78f4;
  8. border-radius: 2px;
  9. -moz-box-shadow: inset 0 0 4px #06c;
  10. -webkit-box-shadow: inset 0 0 4px #06c;
  11. box-shadow: inset 0 0 4px #06c;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <input onclick="doClick(this,'ddd(aa/bb/cc/erererer/dfdfdfdfdfdfdfdfdfdfdfdfdf/ejjejrjerjer//erererer ////////)eee')" />
  17. <input onclick="doClick(this,'ddd(aa/bb/cc/erererer/dfdfdfdfdfdfdfdfdfdfdfdfdf/ejjejrjerjer//erererer ////////)eee')" style='width: 300px;' />
  18. <p><b>Needed Properties:</b> sender.offsetTop & sender.offsetLeft & sender.offsetHeight</p>
  19. </body>
  20. </html>
  21. <script type="text/javascript">
  22. function delGridComboBox() { // 删除弹出框
  23. var divContainer = document.getElementById('gridComboBox');
  24. if (divContainer) {
  25. divContainer.parentNode.removeChild(divContainer);
  26. }
  27. }
  28. function doClick(sender, str) {//str='ddd(aa/bb/cc/erererer/dfdfdfdfdfdfdfdfdfdfdfdfdf/ejjejrjerjer//erererer ////////)eee' 只取()得内容/分割做为数据源
  29. delGridComboBox();
  30. // console.log(sender);
  31. // for(var i in sender)
  32. // {
  33. // console.log(i+"|"+sender[i]);
  34. // }
  35. var re = /[^\)\(]*/g //取出()中的内容作为下拉数据源
  36. var fit = str.match(re);
  37. var fmt = fit[2].split('/');
  38. var divContainer = document.createElement('div');
  39. divContainer.style.width = sender.clientWidth + 2 + "px";
  40. divContainer.style.overflow = "hidden";
  41. divContainer.style.position = 'absolute';
  42. divContainer.style.top = sender.offsetTop + sender.offsetHeight + 'px';
  43. divContainer.style.left = sender.offsetLeft + 'px';
  44. divContainer.style.zIndex = 999;
  45. divContainer.id = "gridComboBox";
  46. for (var i = 0; i < fmt.length; i++) {
  47. console.log('fmt[i]>>', fmt[i]);
  48. var txt = document.createElement('div');
  49. txt.innerHTML = fmt[i];
  50. txt.title = txt.innerHTML;
  51. txt.style.margin = 3+'px';
  52. txt.addEventListener('mouseover', function changeBg(event) {
  53. event.target.style.fontWeight = 'bold';
  54. event.target.style.color = 'white';
  55. event.target.style.backgroundColor = '#2d78f4';
  56. }, false);
  57. txt.addEventListener('mouseout', function unChangeBg(event) {
  58. event.target.style.fontWeight = 'normal';
  59. event.target.style.color = 'black';
  60. event.target.style.backgroundColor = 'white';
  61. }, false);
  62. txt.onclick = function(subSender) {
  63. sender.value = subSender.target.innerText;
  64. delGridComboBox();
  65. };
  66. divContainer.appendChild(txt);
  67. };
  68. document.body.appendChild(divContainer);
  69. };
  70. </script>

如果大家还想深入学习,可以点击jquery下拉框效果汇总、JavaScript下拉框效果汇总进行学习。

以上就是js实现select下拉框菜单的代码,希望本文所述对大家学习javascript程序设计有所帮助。

人气教程排行