IE678不支持<option>style="display:none"

<! DOCTYPE  html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="yue-Hans" lang="yue-Hans">
< head >
     < meta  http-equiv="Content-Type" content="text/html; charset=gbk" />
     < title >it works..............</ title >
</ head >
< body >
< form  action="./" method="get">
     < dl >
         < dt >发布者</ dt >
         < dd >
             < select  name="q_role" onchange="role_change_type();">
                 < option  value="company">中介</ option >
                 < option  value="person">个人</ option >
             </ select >
             < select  name="q_type">
                 < option  value="sale">出售</ option >
                 < option  value="lease">出租</ option >
                 < option  value="buy" style="display:none">求购</ option >
                 < option  value="hire" style="display:none">求租</ option >
             </ select >
         </ dd >
     </ dl >
</ form >
</ body >
</ html >
想实现一个很简单的功能:当选中“中介”时,不显示“求购”与“求租”。本以为通过display:none即可实现,结果发现在option元素上使用display:none在firefox中有效,在IE6、IE7、IE8中都无效。
  
  
  所以,通过javascript设置display:none也是在IE中无效,代码如下:
<! DOCTYPE  html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="yue-Hans" lang="yue-Hans">
< head >
     < meta  http-equiv="Content-Type" content="text/html; charset=gbk" />
     < title >it works..............</ title >
</ head >
< body >
< form  action="./" method="get">
     < dl >
         < dt >发布者</ dt >
         < dd >
             < select  name="q_role" onchange="role_change_type();">
                 < option  value="company">中介</ option >
                 < option  value="person">个人</ option >
             </ select >
             < select  name="q_type">
                 < option  value="sale">出售</ option >
                 < option  value="lease">出租</ option >
                 < option  value="buy">求购</ option >
                 < option  value="hire">求租</ option >
             </ select >
         </ dd >
     </ dl >
     < script  type="text/javascript">
     function role_change_type()
     {
         var q_role=document.getElementsByName("q_role");
         var q_type=document.getElementsByName("q_type");
         var q_type_option=q_type[0].getElementsByTagName("option");
         if(q_role[0].value=='company')
         {
             if(q_type[0].value=='buy'||q_type[0].value=='hire')
             {
                 q_type[0].value='sale';
             }
             for(var i=0;i!=q_type_option.length;i++)
             {
                 if(q_type_option[i].value=="buy"||q_type_option[i].value=="hire")
                 {
                     q_type_option[i].style.display="none";
                     
                 }
             }
         }
         if(q_role[0].value=='person')
         {
             for(var i=0;i!=q_type_option.length;i++)
             {
                 q_type_option[i].style.display="";
             }
         }
     }
     role_change_type();
     </ script >
</ form >
</ body >
</ html >
  
所以,只能通过select元素的remove和add方法,当选中“中介” 时,把“求租”和“求购”删除。代码如下:
<! DOCTYPE  html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="yue-Hans" lang="yue-Hans">
< head >
     < meta  http-equiv="Content-Type" content="text/html; charset=gbk" />
     < title >it works..............</ title >
</ head >
< body >
< form  action="./" method="get">
     < dl >
         < dt >发布者</ dt >
         < dd >
             < select  name="q_role" onchange="role_change_type();">
                 < option  value="company">中介</ option >
                 < option  value="person">个人</ option >
             </ select >
             < select  name="q_type">
                 < option  value="sale">出售</ option >
                 < option  value="lease">出租</ option >
                 < option  value="buy">求购</ option >
                 < option  value="hire">求租</ option >
             </ select >
         </ dd >
     </ dl >
     < script  type="text/javascript">
     var q_role=document.getElementsByName("q_role");
     var q_type=document.getElementsByName("q_type");
     var q_type_option=q_type[0].getElementsByTagName("option");
     alert(q_role[0].value)
     if(q_role[0].value=='company')
     {
         q_type[0].remove(3)
         q_type[0].remove(2)
     }
     function role_change_type()
     {
         if(q_role[0].value=='company')
         {
             q_type[0].remove(3)
             q_type[0].remove(2)
         }
         if(q_role[0].value=='person')
         {
             var x=document.createElement('option');
             x.text='求购';
             x.value='buy';
             var y=document.createElement('option');
             y.text='求租';
             y.value='hire';
             try
             {
             q_type[0].add(x,null);
             q_type[0].add(y,null); // standards compliant
             }
             catch(ex)
             {
             q_type[0].add(x);
             q_type[0].add(y); // IE only
             }
         }
     }
     </ script >
</ form >
</ body >
</ html >
这样在IE和firefox中都有效了。真费解啊,IE8竟然还不支持option的display:none 。
  上面的代码还有一个问题:在IE7和IE8中 选中“个人”,然后刷新,将导致“求租”和“求购”被删除。在IE6和firefox3.5.5中正常。
标签: javascript, css, ie
 
 
< optgroup  value = "3">4</ optgroup >   直接隐藏!
给下面代码补充一个样式:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>计时器</title> <link rel="preload" href="fonts/mui.ttf" as="font" crossorigin> <link rel="stylesheet" href="vendor/mui.min.css"> <link rel="stylesheet" href="vendor/waves.min.css"> <link rel="stylesheet" href="static/general-48e00d0585.css"> <link rel="shortcut icon" href="https:assets.retiehe.com/rth-legacy-icom-512.png"> <script src="static/theme-d5fe8b3fc6.js"></script> <meta name="theme-color" content="#ffffff"> </head> <body class="timer"> <div id="bg-img" class="bg-img"></div> <main> <div id="time" data-v-app> <div class="mui-input-row"> <input class="display" type="text"> </div> <div class="mui-input-row mui-input-range"> <label for="hour">时</label> <input id="minute" tpye="range" min="0" max="59"> </div> <div class="mui-input-row mui-input-rang"> <label for="second">秒</label> <input id="second" type="range" min="0" max="59"> </div> </div> <select> <option value="Countdown">倒计时</option> <option value="Stopwatch">秒表</option> <option value="CurrentTime">当前时间</option> </select> <button id="star-btn" type="button" class="mui-bin mui-btn-blue mui-btn-block waves-effect wave-light">开始</button> <button id="reset-btn" tpye="button" class="mui-btn mui-btn-block waves-effect waves-light">复位</button> </main> <script scr="static/i18n-36955081a6.js"></script> <script scr="static/general-e70b71a82f.js"></script> <script scr="https://static.retiehe.com/js/waves.min.js"></script> <script scr="static/timer-181c3930f6.js"></script> <script async scr="https://static.retiehe.com/js/wave.min.js"></script> <header class="mui-bar mui-bar-nav"> <div id="back-btn" class="mui-icon mui-icon-contact mui-pull-right" tabindex="0" role="button" aria-label="用户"></div> </header> <div class="mask" tabindex="0" aria-label="关闭弹窗" hidden></div> </body> </html>
最新发布
05-31
<!DOCTYPE html> <html> <head> <title>表格形式</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> table, th, td { border: 1px solid black; border-collapse: collapse; padding: 5px; } </style> <script> // 添加一行 function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); for(var i = 0; i < 5; i++){ var cell = row.insertCell(i); cell.innerHTML = '<select><option value="1">选项1</option><option value="2">选项2</option><option value="3">选项3</option></select>'; cell.onclick = function(){ this.firstChild.style.display = 'block'; } cell.firstChild.onblur = function(){ this.style.display = 'none'; } cell.firstChild.style.display = 'none'; } } </script> </head> <body> <table id="myTable"> <thead> <tr> <th>列1</th> <th>列2</th> <th>列3</th> <th>列4</th> <th>列5</th> </tr> </thead> <tbody> <tr> <td><select><option value="1">选项1</option><option value="2">选项2</option><option value="3">选项3</option></select></td> <td><select><option value="1">选项1</option><option value="2">选项2</option><option value="3">选项3</option></select></td> <td><select><option value="1">选项1</option><option value="2">选项2</option><option value="3">选项3</option></select></td> <td><select><option value="1">选项1</option><option value="2">选项2</option><option value="3">选项3</option></select></td> <td><select><option value="1">选项1</option><option value="2">选项2</option><option value="3">选项3</option></select></td> </tr> </tbody> </table> <button onclick="addRow('myTable')">增加</button> </body> </html>将这段代码用bootstrap进行优化
04-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值