`
caniggia1986
  • 浏览: 149745 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ie6 getElementById/getElementsByName bug

 
阅读更多
javascript:mctmp(0);

in ie6:


document.getElementsByName(nameValue) will return the element which id attribute equals to nameValue

document.getElementById(idValue) will return the element which name attribute equals to idValue

so prevent to use the same id and name attribute value in dom!


var getElementById = function(id){
  var el = document.getElementById(id);
  if(!+"\v1"){//ie
    //直接用el.id的方式来取值,无论是在IE还是标准浏览器,都无法取得正确值,而是返回元素本身的toString()。
    if(el && el.attributes['id'].value === id){
      return el
    }else{
      var els = document.all[id],n = els.length;
      for(var i=0;i<n;i++){
        if(els[i].attributes['id'].value === id){
          return els[i]
        }
      }
    }
  }
  return el;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics