---
title: "【引用】js中firefox,ie兼容"
author: "zithan"
date: "2011-06-29T15:32:07Z"
tags: []
categories: ["Web前端"]
slug: "js-firefox-ie"
draft: false
lastmod: "2011-06-29T15:32:07Z"
id: 1109443
url: "https://zithan.iteye.com/blog/1109443"
---

### 【引用】js中firefox,ie兼容

2011-03-24 13:41:23|  分类： [默认分类](http://qiyuren20.blog.163.com/blog/#m=0&t=1&c=fks_087071087081086064084095080095085087085070081094080067092 "默认分类") |  标签：无|字号 [订阅]()

本文引用自竹林之音[《js中firefox,ie兼容》](http://blog.163.com/liguiqin_500/blog/static/763064200982410565999)

1.动态给添加行，其中第一列放checkbox  
  var tbl = document.getElementById("aclTbl"); （通过id获得table）   
  //插入一行  
  var rowsLen = tbl.rows.length;  
  var row = tbl.insertRow(rowsLen);  
  aa. firefox写法：  
     var chkbox = document.createElement("input");  
     chkbox.type = "checkbox";  
     chkbox.name = "checkbox\_index";  
     chkbox.value = rowsLen;  
     cell = row.insertCell(0);  
     cell.appendChild(chkbox);  
     row.appendChild(cell);   
  bb. ie写法：  
    var chkbox = document.createElement("&lt;input type='checkbox' name='checkbox\_index' value='" + rowsLen +   "'/&gt;");    
    cell = row.insertCell(0);  
    cell.appendChild(chkbox);  
    row.appendChild(cell);  
 cc. 兼容写法:  
    var chkbox= document.createElement("td");  
    chkbox.innerHTML = "&lt;input type='checkbox' name='checkbox\_index' value='" + rowsLen + "'/&gt;";  
    cell = row.insertCell(0);  
    cell.appendChild(chkbox);  
    row.appendChild(cell); 

2.动态修改表格本内容  
   tbl.rows\[row\_index].cells(1).innerHTML = acl\_protocol;    // ie支持,firefox不支持  
   tbl.rows\[row\_index].cells\[1].innerHTML = acl\_protocol;    // ie,firefox兼容

3\. ie支持直接通过id找到元素，firefox必须通过document.getElementById(idName)找到元素。

4\. 当同名checkbox只有一个的时候，在firefox中document.getElementById(checkbox\_name).length=undifined.在IE中测试时，document.getElementById(checkbox\_name).length有时为1，有时为undifined，不知是什么原因。

5.在IE中经常定义var $ = document.getElementById，后面就用$代替document.getElementById，如var temp = $("test");。我在firefox中使用这种方法提示错误，后来将var temp = $("test")改为var temp = document.getElementById("test"),运行正常。