欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IE8/IE7/IE6下Select列表中disabled的使用

程序员文章站 2022-06-26 16:14:59
...
昨天项目中使用select列表list-to-list,其中右边有一项是不可以让用户选中,就是必须选,我使用了selectedFields.options[0].disabled = 'disabled';或者selectedFields.options[0].disabled = true;在IE8下面是正常的,不可选的,但在IE6和IE7下没有起到作用,试了几种方法都不行,最后在网上找到下面代码,只要copy到你的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++) {
if(select.id == "your select id") {[//你的select id
select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
select.onchange = function(){ restore(this); }
emulate(select);
[color=red]}[/color]}
}
}
}

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";
}
}
}
相关标签: JavaScript