JavaScript 触发click事件 兼容FireFox,IE 和 Chrome

<script language="javascript">  
    function test2(name)  
    {  
        if(document.all)  
        {  
            document.getElementById(name).click();  
        }  
        else  
        {  
            var evt = document.createEvent("MouseEvents");  
            evt.initEvent("click", true, true);  
            document.getElementById(name).dispatchEvent(evt);  
        }  
    }  
</script>  
  
<a href="#" id="a3" onclick="location.href = 'http://www.baidu.com';">baidu</a>  
<input type="button" value="clck" onclick="test2('a3');"/>

转载:http://justcoding.iteye.com/blog/688467

相关推荐