js代码如下:
document.getElementById(“id”).click()在ie中可行,但是在火狐中不行,请问要怎么写才可以都兼容。

解决方案 »

  1.   


    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> 
    <script src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
    window.onload=function(){
    if(document.all) document.getElementById("pad").click();
    else document.getElementById("pad").onclick();
    }
    </script>
    </head>
    <body>
      <div id="pad" onclick="alert('ok');"></div>  
    </body>
    </html>
      

  2.   


    不好意思,火狐下不行换一个if(document.all)
    {
    document.getElementById(elmid).click();
    }
    else
    {
    var evt = document.createEvent("MouseEvents");
    evt.initEvent("click", true, true);
    document.getElementById(elmid).dispatchEvent(evt);

      

  3.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <HTML>
    <head>
    <script>
    if(!HTMLElement.click)
    HTMLElement.prototype.click = function(){
    this.onclick();
        }

    function se(){
    alert('a');
    }function haha(){
    document.getElementById('ss').click();
    }
    </script>
    </head>
    <BODY>
    <div id='ss' style="height:100px; width:100px; background-color:#000000" onClick="se()"> </div>
    <input  value="额" type="button" onClick="haha()">
    </BODY>
    </HTML>
      

  4.   

    click不是每个元素都有的,而且getElementById可能返回null
      

  5.   

    用jquery试试:
    $("#id").bind("click",function (){......});
    应该就可以完全兼容了。
      

  6.   


    参见:initEvent,dispatchEvent