---
title: "重写getElementById  兼容Ie 6 8 7  火狐"
author: "leyoo"
date: "2011-02-21T09:22:47Z"
tags: []
categories: ["Web前端"]
slug: "getelementbyid-ie-6-8-7"
draft: false
lastmod: "2011-02-21T09:22:47Z"
id: 911534
url: "https://leyoo.iteye.com/blog/911534"
---

<script   language="JavaScript">    
    //重写getElementById  兼容Ie 6 8 7  火狐
    document.nativeGetElementById = document.getElementById;   
    document.getElementById = function(id)   
    {   
        var elem = document.nativeGetElementById(id);   
        if(elem)    //兼容IE  Version 6 7 8
        {   
            //修改后的确认能得到id属性方法   
            if(elem.attributes.value == id)   
            {   
			  
                return elem;   
            }  
			else  
		    {   
				if(document.getElementsByName(id)!=null&&document.getElementsByName(id).length>0)
				{
					 return document.getElementsByName(id);
				}
		     }   
        }   
		else  //兼容fox
		{   
			if(document.getElementsByName(id)!=null&&document.getElementsByName(id).length>0)
			{
				 return document.getElementsByName(id);
			}
		}   
        return null;   
    };  
</script>