`
hideto
  • 浏览: 2699405 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

IE下不能disabled掉select标签的option的解决方案

阅读更多
原文:Select, Option, Disabled And The JavaScript Solution
今天在工作中需要用到disabled掉一些select的option,结果发现IE6没有实现它。
还好我们下载select-option-disabled-emulation.js文件即可自动完成disabled标记的工作,代码也很少:
/****************************************************************
* 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";
		}
	}
}
分享到:
评论
1 楼 f0799 2009-09-22  
如果使用JS如$('s2')[index].disabled="disabled";js将不起作用

相关推荐

    IE6中select标签的option不能disabled的解决方案

    在早期的Internet Explorer 6(简称IE6)浏览器中,存在一个众所周知的兼容性问题:`&lt;select&gt;`标签内的`&lt;option&gt;`元素无法正确地应用`disabled`属性。这个问题使得开发者无法在IE6中实现禁用某些选项的功能,这对于...

    用jQuery解决IE不支持的option disable属性

    总结起来,通过使用jQuery,我们可以优雅地解决IE浏览器不支持`&lt;option&gt;`的`disabled`属性的问题。上述代码通过CSS样式模拟禁用效果,并结合JavaScript逻辑,确保在用户尝试选择禁用选项时,选中状态不会改变。这个...

    SELECT在浏览器中相关Bug整理

    本文将详细讨论`SELECT`在不同浏览器中遇到的一些常见Bug及其解决方案。 首先,针对IE6的一个突出问题是,`SELECT`元素的`z-index`属性失效,导致它总是位于其他元素之上,无法被覆盖。这个问题可以通过使用JQuery...

    python selenium运行失败常见错误.docx

    Python Selenium 运行失败常见错误解决方案 在使用 Python Selenium 进行自动化测试时,经常会遇到一些常见的错误,这些错误可能是由于各种原因引起的,如元素定位不到、驱动版本不匹配、浏览器版本不兼容等等。...

    Selecter:用于替换默认选择元素的 jQuery 插件

    **jQuery Selecter插件详解** `Selecter`是一款基于jQuery的插件,旨在提供一个更加美观、功能丰富的选择元素替代方案。...对于那些寻求更强大、更现代选择器解决方案的开发者,可以考虑转向其他活跃维护的库。

Global site tag (gtag.js) - Google Analytics