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

来源:互联网 发布:淘宝旺铺模板 编辑:程序博客网 时间:2024/06/08 13:36

使用 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.csdn.net/btbtd

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


原创粉丝点击