原文:
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";
}
}
}
分享到:
相关推荐
在早期的Internet Explorer 6(简称IE6)浏览器中,存在一个众所周知的兼容性问题:`<select>`标签内的`<option>`元素无法正确地应用`disabled`属性。这个问题使得开发者无法在IE6中实现禁用某些选项的功能,这对于...
总结起来,通过使用jQuery,我们可以优雅地解决IE浏览器不支持`<option>`的`disabled`属性的问题。上述代码通过CSS样式模拟禁用效果,并结合JavaScript逻辑,确保在用户尝试选择禁用选项时,选中状态不会改变。这个...
本文将详细讨论`SELECT`在不同浏览器中遇到的一些常见Bug及其解决方案。 首先,针对IE6的一个突出问题是,`SELECT`元素的`z-index`属性失效,导致它总是位于其他元素之上,无法被覆盖。这个问题可以通过使用JQuery...
Python Selenium 运行失败常见错误解决方案 在使用 Python Selenium 进行自动化测试时,经常会遇到一些常见的错误,这些错误可能是由于各种原因引起的,如元素定位不到、驱动版本不匹配、浏览器版本不兼容等等。...
**jQuery Selecter插件详解** `Selecter`是一款基于jQuery的插件,旨在提供一个更加美观、功能丰富的选择元素替代方案。...对于那些寻求更强大、更现代选择器解决方案的开发者,可以考虑转向其他活跃维护的库。