很多时候我都要同时兼容IE和火狐,在网上看了很多,乱七八糟,没个整体的概念,那位大侠能帮我支招啊,顺便问一个问题
<script language="javascript">
    //js动态添加表格行
    var i;
    function addRow() {
        debugger;
        var doc;
        if (document.all) { //IE
            doc = document.frames["MyFrame"].document;
        }
        else {
            doc = document.getElementById("MyFrame").contentDocument;
        }
        var tableid = doc.getElementById("mytable");
        //window.parent.window.frames[0].document.getElementById("mytable");
        var newTr = tableid.insertRow();
        var newTd0 = newTr.insertCell();
        var newTd1 = newTr.insertCell();
        var newTd2 = newTr.insertCell();
        var newTd3 = newTr.insertCell();
        var newTd4 = newTr.insertCell();
        var newTd5 = newTr.insertCell();
        var newTd6 = newTr.insertCell();
        var newTd7 = newTr.insertCell();
//        for (i = 0; i < 8; i++) {
//            var str = "newTd" + i;
//            str = newTr.insertCell();
//        }
        newTd0.innerHTML = "你好";
    }
</script>IE下运行正常,火狐下运行错误,这是什么原因啊?

解决方案 »

  1.   

    如果你想一劳永逸,那么学jQuery用它,这东西上手很快否则只有遇到一个写个兼容函数
      

  2.   

    另外这个是不是你说的效果?<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>    <script type="text/javascript">
            var data = { "百度": ["http://www.baidu.com"], "新浪": ["http://www.sina.com"] };
            window.onload = function() {
                var table = document.createElement("table");
                table.border = "1";
                table.id = "table1";
                document.body.appendChild(table);
            };
            function addweb() {
                var table = document.getElementById("table1");
                for (var website in data) {
                    var tr = document.createElement("tr");
                    var td1 = document.createElement("td");
                    var td2 = document.createElement("td");
                    td1.innerHTML = website;
                    td2.innerHTML = "<a href='" + data[website] + "'>" + data[website] + "</a>";
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    table.appendChild(tr);
                }
            }
        </script></head>
    <body>
        <input type="button" id="Button2" value="增加网站" onclick="addweb()" />
    </body>
    </html>
      

  3.   

    将下面这段
    function addRow() {
      debugger;
      var doc;
      if (document.all) { //IE
      doc = document.frames["MyFrame"].document;
      }
      else {
      doc = document.getElementById("MyFrame").contentDocument;
      }
    改为
    function addRow() {
      var doc;
      doc = document.getElementById("MyFrame").contentWindow.document;
    试试