以下脚本在IE6、IE7、IE8 Beta2、Firefox 3、Chrome等浏览器中测试通过。

Javascript 脚本:

 FireEvent(controlID, eventName)
{
    if (document.all)    // For IE.
    {
        eval(
"document.getElementById(\"" + controlID + "\")." + eventName + "();");
    }
    
else    // For Nescape
    {
        
var e = document.createEvent('HTMLEvents');
        e.initEvent(eventName, 
falsefalse);
        document.getElementById(controlID).dispatchEvent(e);
    }
}

 

 HTML测试脚本:


 <HEAD>
  
<TITLE> New Document </TITLE>
  
<META NAME="Generator" CONTENT="EditPlus">
  
<META NAME="Author" CONTENT="">
  
<META NAME="Keywords" CONTENT="">
  
<META NAME="Description" CONTENT="">
  
<script type="text/javascript">
  
function DoClick()
  {
      FireEvent(
"button2""click");
  }

  
function Display(control)
  {
    alert(control.value);
  }

  
function FireEvent(controlID, eventName)
  {
    
if (document.all)    // For IE.
    {
        eval(
"document.getElementById(\"" + controlID + "\")." + eventName + "();");
    }
    
else    // For Nescape
    {
        
var e = document.createEvent('HTMLEvents');
        e.initEvent(eventName, 
falsefalse);
        document.getElementById(controlID).dispatchEvent(e);
    }
  }
  
</script>
 
</HEAD>

 
<BODY>
    
<input id="button1" type="button" value="I'm Button1." onclick="DoClick();" />
    
<input id="button2" type="button" value="I'm Button2." onclick="Display(this);" /> 
 
</BODY>
</HTML>

 

相关文章:

  • 2022-12-23
  • 2021-07-08
  • 2022-12-23
  • 2021-10-14
  • 2021-08-25
  • 2021-10-25
  • 2022-12-23
猜你喜欢
  • 2021-10-31
  • 2021-07-04
  • 2021-08-09
  • 2022-01-28
  • 2022-12-23
  • 2022-12-23
  • 2021-07-12
相关资源
相似解决方案