`

IE不支持option的display样式,只能使用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" 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
 
 
<a href="http://www.cnblogs.com/sink_cup/archive/2009/12/15/ie_option_display_none_select_remove_add_html_dom.html">http://www.cnblogs.com/sink_cup/archive/2009/12/15/ie_option_display_none_select_remove_add_html_dom.html</a>
<optgroup value = "3">4</optgroup>   直接隐藏!
分享到:
评论

相关推荐

    simple-tags

    * Add an option for choose input text or textarea for old tags field * Add an option for min chars autocompletion * Version 2.0-beta8 : * Update POT. * Update french translation. * Version 2.0-...

    端口查看工具

    o Added 'Add Header Line To CSV/Tab-Delimited File' option. When this option is turned on, the column names are added as the first line when you export to csv or tab-delimited file. * Version 1.82...

    Select精美下拉框(漂亮)

    this.options.add(NewOption, idx); NewOption.innerText = innerText; NewOption.value = value; if (!this.bOriginalSelect) this.createOptionTr(idx); this.syncOptions(); this.adjustOptionsDiv(); ...

    gerrit-3.0.3.war

    Issue 11137: Remove the gerrit.reportBugText configuration option. This option was only used in GWT, which has been removed. Upgrade JGit to 5.3.5.201909031855-r. This version includes a fix for ...

    ASP.NET Gridview隐藏/显示列源码

    当列头的hyperlink被点击的时候后,它将会传递GridView的名字,列的索引和列名给HideCol方法,这个方法能找到这一列的每个单元格,每个单元格的将添加display:none样式,用来隐藏这一列. 当选择"Show Column"中的...

    FairyGUI-Unity-Plugin-3.4.0.zip

    - NEW: Add multi-display support. - NEW: Add API DynamicFont(name, font). - IMPROVED: Compatibility with 2018.3. - FIXED: Incorrect letter spacing on mobile platform. - FIXED: Same transition hook may...

    thinkphp 3.2 省市区联动

    * 那么如果存在时就加不上去(数量),如果不存在时(如果不存在时的数量不一样时)就insert上去 * */ $province = M('prvices')-&gt;where ( array('pid'=&gt;1) )-&gt;select (); $this-&gt;assign('province',$...

    OutlookAttachView v2.73

    o Added option to choose the file size display unit: Bytes, KB, or MB. * Version 2.50 o OutlookAttachView now remembers that last 20 strings you typed in the Extensions List, Excluded Extensions ...

    pdf修改软件

    Removes all the evaluation marks on a PDF file that was added by Foxit PDF Editor 2.1 all at once by selecting "Remove Evaluation Marks" under the Help menu for registered users. 5. Alignment Support...

    wrar571.exe

    b) if "Save archive name" and "Add to context menu" options were set in compression profile parameters and profile was invoked from Explorer context menu, WinRAR proposed an automatically ...

    Bochs - The cross platform IA-32 (x86) emulator

    - Added write protect option for floppy drives. - Bugfixes / improved internal debugger + instrumentation. Detailed change log : - CPU and internal debugger - Implemented Process Context ID (PCID) ...

    8-07-14_MegaCLI for linux_windows

    LSIP200245968 (DFCT) In EFICLI not able to flash latest firmware to controller without using -nosigchk -noverchk option. LSIP200233079 (DFCT) Redesign battery changes LSIP200232955 (DFCT) Porting ...

    win 3.11 for workgroup tcpip支持

    to add the Microsoft TCP/IP-32 drivers by following the instructions given in the documentation. Known Problems -------------- There have been a number of reports on IBM TokenRing, EtherLink III ...

    uCOS-II v2.52 在 STM32 上的移植

    Remove the incorrect use of SPI in LCD display, add a new schedule( maily used SPI busy flag ). 2. Commen off code "assert_param(IS_SPI_ALL_PERIPH(SPIx));" in "SPI_I2S_SendData()", ...

    Bloodshed Dev-C++

    * Added CVS commands "Add" and "Remove" * Added configuration option for "Templates Directory" in "Environment Options" * Code-completion updates * Bug fixes Version 4.9.6.6 * Editor colors are ...

    WordPress 2.9 E-Commerce.pdf

    Remove the sidebar Google Checkout button 186 Summary 187 Chapter 9: Deploy, Secure, and Maintain Your Shop 189 Getting ready to deploy 189 Necessary WordPress adjustments 190 Your WordPress ...

    ImageMagick图片批量处理

    -alpha option activate, deactivate, reset, or set the alpha channel -antialias remove pixel-aliasing -authenticate password decipher image with this password -attenuate value lessen (or intensify...

    Windows2000活动目录开发人员参考库第4卷ADSI参考手册

    第1章 活动目录库套书的使用 1.1 活动目录库套书的组成 1.2 活动目录库套书的编写思想 第2章 本书的内容 2.1 组策略编程 第3章 微软参考资源 3.1 微软开发者网络 3.1.1 MSDN和MSDN在线的比较 3.1.2 订阅MSDN...

Global site tag (gtag.js) - Google Analytics