这2天发现以前在IE6,IE7等浏览器下运行正常的程序在IE8下都不能正常运行
通过VS.Net调试发现问题所在,IE8的getElementById只支持id,不支持name

在原来IE7及以前的版本中,类似
<input type="text" name="txt1">
都可以使用
var obj = document.getElementById("txt1");来取得对象

IE8中不知道为什么取消了这个功能
var obj = document.getElementById("txt1");
此时obj会等于null

我的解决方法:
// 用$ 代替 document.getElementById
function $(id){
    if (typeof(id)=="object")
        return id;
    if (typeof(id)=="string"){
        var obj = document.getElementById(id);
        if(obj != null)
            return obj;
        obj = document.getElementsByName(id);
        if(obj != null && obj.length > 0)
            return obj[0];
    }
    return null;
}

 

相关文章:

  • 2022-12-23
  • 2021-07-24
  • 2021-05-10
  • 2021-09-06
  • 2021-06-28
  • 2022-12-23
  • 2021-11-18
  • 2021-08-20
猜你喜欢
  • 2021-08-16
  • 2021-12-28
  • 2022-12-23
  • 1970-01-01
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
相关资源
相似解决方案