惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
V
Visual Studio Blog
Jina AI
Jina AI
博客园 - 司徒正美
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
小众软件
小众软件
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
WordPress大学
WordPress大学
爱范儿
爱范儿
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏

博客园 - 活力豆

Javascript和CSS在线格式化工具 SQL Server数据归档的解决方案 Web开发员必须有的5个Firefox插件 FileSystemWatcher触发多次Change事件的解决办法 jQuery.unbind()出现不工作的解决办法 MVC轻量级的js库:BackBone.js 关于数据可视化方面的文章列表(搜集中) 带有进度条的jQuery文件上传插件 在ASP.NET MVC 3上使用依赖注入更加容易了 学习EF4的一些基础参考资料 解决在安装了IE7机子上要用IE6测试的问题 CSS 方面的资料 数据格式化 Windows Community Foundation学习资源 Windows WorkFlow Foundation学习资源 序列化方面的文章 - 活力豆 - 博客园 DHTML 站点 Web Menu 相关的文章 Web图片显示相关的文章
如何在IE6下,应用disabled到Select标签的Option上的方法
活力豆 · 2011-04-06 · via 博客园 - 活力豆

在开发web程序的时候,我需要将页面下拉框<select>的某个选项<option>给关闭掉,即用户可以看到但是不能选择这个选项,我们可以这样做:

<select>
  
<option value="volvo">Volvo</option>
  
<option value="saab">Saab</option>
  
<option value="mercedes" disabled>Mercedes</option>
  
<option value="audi">Audi</option>
</select>

即通过"disabled"可以让选项处于关闭状态,用户不能选择。但是头疼的问题来了,IE7以上版本支持,在IE6下,"disabled"是不起作用的,有一个解决方案Select, Option, Disabled And The JavaScript Solution 是通过javascript模拟关闭状态,效果不错。

select-option-disabled-emulation.js 文件如下:

/****************************************************************
* Author:    Alistair Lattimore
* Website:    http://www.lattimore.id.au/
* Contact:    http://www.lattimore.id.au/contact/
*            Errors, suggestions or comments
* Date:        30 June 2005
* Version:    1.0
* Purpose:    Emulate the disabled attributte for the <option> 
*            element in Internet Explorer.
* Use:        You are free to use this script in non-commercial
*            applications. You are however required to leave
*            this comment at the top of this file.
*
*            I'd love an email if you find a use for it on your 
*            site, though not required.
***************************************************************
*/

window.onload 

= function() {
    
if (document.getElementsByTagName) {
        
var s = document.getElementsByTagName("select");if (s.length > 0) {
            window.select_current 
= new Array();for (var i=0, select; select = s[i]; i++) {
                select.onfocus 
= function(){ window.select_current[this.id] = this.selectedIndex; }
                select.onchange 
= function(){ restore(this); }
                emulate(select);
            }
        }
    }
}
function restore(e) {
    
if (e.options[e.selectedIndex].disabled) {
        e.selectedIndex 
= window.select_current[e.id];
    }
}
function emulate(e) {
    
for (var i=0, option; option = e.options[i]; i++) {
        
if (option.disabled) {
            option.style.color 
= "graytext";
        }
        
else {
            option.style.color 
= "menutext";
        }
    }
}

然后再HTML页面上增加下面的代码

 <!--[if lt IE 7]>
 <script type="text/javascript" src="select-option-disabled-emulation.js"></script>
 <![endif]
-->

在IE6下我们看到,被"disabled"的选项处于关闭状态,用户选择后会自动恢复到前一个状态项。