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

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - 活力豆

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"的选项处于关闭状态,用户选择后会自动恢复到前一个状态项。