javascript firefox中onclick()函数不能用的问题


<a id="downloadFileLink" href="1.html" ></a>

var downloadLink = document.getElementById('downloadFileLink');
downloadLink.onclick();  //this sentence can't run in firefox

解决办法:
标签中添加onclick事件<a id="downloadFileLink" href="1.html" onclick="alert('a');" ></a>

 var downloadLink  =  document.getElementById( ' downloadFileLink ' );
        downloadLink.onclick 
=  Function( " location.href='DownloadHandler.ashx?fileName= "   +  fileName  +   " &filePath= "   +  filePath  +   " ' " );
        downloadLink.style.display 
=   ' block ' ;
        downloadLink.style.display.visibility 
=   ' hidden ' ;
        
// downloadLink.onclick();   // this sentence can't run in firefox
         if  (document.all  &&   typeof  (document.all)  ==   " object " )    // IE  
        {
            downloadLink.fireEvent(
" onclick " );
        }
        
else  {
            var e 
=  document.createEvent( ' MouseEvent ' );
            e.initEvent(
' click ' false false );
            downloadLink.dispatchEvent(e);
        }
        
return   false ;


你可能感兴趣的:(JavaScript)