firefox下模拟click事件

来源:互联网 发布:2018人工智能国际大会 编辑:程序博客网 时间:2024/04/30 09:20

 <a   href="#"   onclick="test(1)"   id="a3">hello</a>  
  <a   href="#"   onclick="test2(1)"   id="b3">hello2</a>  
  <script   language="javascript">  
  <!--  
  function   test(num)  
          {  
                  window.alert(num);  
          }  
          function   test2(num)  
          {  
                    if(document.all)  
                    document.getElementById("a3").click();          
    else  
    {  
    var   evt   =   document.createEvent("MouseEvents");  
    evt.initEvent("click",   true,   true);  
    document.getElementById("a3").dispatchEvent(evt);  
    }  
          }  
   
  //-->  

升级你的FIREFOX!  
  opera可以再改一下  

<img   id="a"   src="/a.jpg"   onclick="alert('a');"/><div   onclick="clickObj('a')">click   me</div>  
  <script   language="javascript">  
  <!--  
  function   clickObj(o){  
  var   o   =   document.getElementById(o);  
  if(   document.all   &&   typeof(   document.all   )   ==   "object"   )   //IE  
  {  
  o.fireEvent("onclick");  
  }  
  else  
  {  
  var   e   =   document.createEvent('MouseEvent');  
  e.initEvent('click',false,false);  
  o.dispatchEvent(e);  
  }  
  }  
  //-->  
  </script>

 

Fix for Firefox click() event issue

Firefox does not support the javascript click() event on a hyperlink. So doing something like:

<asp:LinkButton ID="lnkMyButton" runat="server">My Button To Click</asp:LinkButton>
 
<a href="#" onclick="document.getElementById('lnkMyButton').click();">Click this to click the other link!</a>

This will work correctly in Internet Explorer but nothing will happen when you execute this javascript in FireFox.

This is because the javascript line in Firefox has the click() event and Firefox does not understand this event for hyperlinks (which is what an ASP.NET LinkButton control is rendered as on page load):

document.getElementById('lnkMyButton').click();

Workaround 1:

Instead of using a LinkButton control, use a Button control instead. Firefox does support the click() event for input type=button HTML controls. So you could change the code to this:

<asp:Button ID="btnMyButton" runat="server" />
 
<a href="#" onclick="document.getElementById('btnMyButton').click();">Click this to click the other button!</a>

Workaround 2:

If you can’t change the element to a Button control instead of a LinkButton, you can simulate the postback event by calling the method that .NET calls in the background when you click on the LinkButton control directly. This event is called __doPostBack() and takes the ID name of the control that you want to cause a postback. So you could change the code to this:

<asp:LinkButton ID="lnkMyButton" runat="server">My Button To Click</asp:LinkButton>
 
<a href="#" onclick="__doPostBack('lnkMyButton', '');">Click this to click the other link!</a>

The nice thing about this one is that even if this is in an ASP.NET AJAX UpdatePanel, it will still do the asynchronous postbacks correctly and post the link button.

原创粉丝点击