使用 js + button标签 模拟 select 列表框 操作 By shawl.qiu

本文介绍了一种使用JavaScript和button标签来模拟select下拉列表的方法,解决了在iframe中使用时焦点转移的问题,并提供了完整的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用 js + button标签 模拟 select 列表框 操作 By shawl.qiu


说明:
我想在列表框中显示文本的时候, 可以用除 option标签 外其他的标签定义显示效果.

但这显然是行不通的, CSS 也无法完成我需要的风格, 翻了一会HTC, 发现有个 public:defaults 属性, 貌似可以完成我需要的效果, 不会用...即使会用, 也不太可能会考虑使用 HTC(只能在IE上跑).

然后就考虑用其他方法模拟 select 操作流程了, 起初是使用 div+js+非控件标签 模拟成功的.
但是有一个问题, 我模拟出来的 select 是针对 iframe 使用的.
期望是, 当我点击模拟的 select 下拉项时, 我在 iframe 里的 焦点不变.
但是, 对于 非控件标签的点击事件, IE 会立即转移页面焦点到非控件标签上. 

因此, 在想了又想后, 才记得 button标签可以包含 HTML 标签, 这不, 就完成我需要的效果了. 

模拟 select 的背景图片: 
http://files.myopera.com/btbtd/albums/165232/select.png
Green Institute

shawl.qiu
2006-11-25
http://blog.youkuaiyun.com/btbtd

使用 js + button标签 模拟 select 列表框 实现代码:
  1. linenum
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns=" http://www.w3.org/1999/xhtml">
  4. <!-- DW6 -->
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <title>shawl.qiu template</title>
  8. <style type="text/css">
  9. /* <![CDATA[ */
  10.     *{
  11.         margin:0px;
  12.         padding:0px;
  13.     }
  14.     .sqEditorCssSelect{
  15.         background: #fff url(sqEditor/images/select.png) no-repeat scroll;
  16.         background-position: right top;
  17.         width:120px;
  18.         height:21px;
  19.         border:1px solid black;
  20.     }
  21.     .sqEditorCssBorder{
  22.         border:1px solid blue;
  23.         margin:0px 0px 2px 0px;
  24.         display:block;
  25.         width:120px;
  26.         background-color:#fff;
  27.     }
  28. /* ]]> */
  29. </style>
  30. <script type="text/javascript">
  31. //<![CDATA[    
  32.     function fSmltSle(obj, e){
  33.         if(!e)var e=window.event;
  34.         var ele=e.target||e.srcElement
  35.         fHidPrnt(obj);
  36.         obj.style.width=ele.offsetWidth+'px'
  37.         obj.style.left=ele.offsetLeft+'px';
  38.         obj.style.top=ele.offsetTop+ele.offsetHeight+'px';
  39.         
  40.         for(var i=0; i<obj.childNodes.length; i++){
  41.             if(obj.childNodes[i].nodeType==1){
  42.                 obj.childNodes[i].className='sqEditorCssBorder';
  43.                 obj.childNodes[i].onclick=function(){
  44.                     fHidPrnt(this.parentNode);
  45.                     ele.innerHTML=this.childNodes[0].nodeName;
  46.                 }
  47.             }
  48.         } // shawl.qiu script
  49.     }
  50.     function fHidPrnt(obj){
  51.         obj.style.display=='none'?obj.style.display='block':obj.style.display='none';
  52.     }    
  53. //]]>
  54. </script>
  55. </head>
  56. <body>
  57. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button class="sqEditorCssSelect" onClick="fSmltSle(document.getElementById('ddFontsize'), event)">just a test</button>
  58. <div style="position:absolute; display:none " id="ddFontsize">
  59.     <button><h1>h1</h1></button>
  60.     <button><h2>h2</h2></button>
  61.     <button><h3>h3</h3></button>
  62.     <button><h4>h4</h4></button>
  63.     <button><h5>h5</h5></button>
  64.     <button><h6>h6</h6></button>
  65. </div>
  66. <div style="text-align:center; ">some text for focus test.</div>
  67. <textarea cols="60" rows="5">some text for focus test.</textarea><br />
  68. <iframe src="about:blank" name="ifm"></iframe>
  69. <script type="text/javascript">
  70. //<![CDATA[
  71.     onload=function(){
  72.         frames['ifm'].document.write('ok');
  73.         frames['ifm'].document.close();
  74.     }
  75. //]]>
  76. </script>
  77. </body>
  78. </html>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值