---
title: "IE6/7中使用setAttribute设置table的cellpadding和cellspacing的Bug"
author: "zhouyrt"
date: "2011-02-23T18:13:47Z"
tags: []
categories: ["Web前端"]
slug: "ie6-7-setattribute-table-cellpadding-cellspacing-bug"
draft: false
lastmod: "2011-02-23T18:20:39Z"
id: 920024
url: "https://snandy.iteye.com/blog/920024"
---

新版邮箱项目中做页签功能时碰到的。

 

table元素的 [cellpadding](http://www.w3.org/TR/html4/struct/tables.html#adef-cellspacing) 和 [cellspacing](http://www.w3.org/TR/html4/struct/tables.html#adef-cellpadding) 属性意义不提了。它们也有对应的css解决方案。如下

```
table {
	border-collapse:collapse;
	border-spacing:0;
}
th,td {
	padding: 0;
}
```

 

JS操作这两个属性有两种方式，

 

**方式1，**

```
table.cellSpacing = 10;
table.cellPadding = 10;
```

   
注意cellSpacing和cellPadding中间的 **S** 和 **P** 都要是大写的。

 

**方式2，setAttribute**

```
table.setAttribute('cellspacing','10');
table.setAttribute('cellpadding','10');
```

   
注意这里的 **s** 和 **p** 都没有大写。

方式2在IE6,7中将不起作用。见附件测试html。需注意。

改成大写后又都可以了

```
table.setAttribute('cellSpacing','10');
table.setAttribute('cellPadding','10');
```

此外，IE开发者工具对于以上两种方式的解析也不同。

table.setAttribute('cellspacing','10') IE6/7 ![](http://dl.iteye.com/upload/attachment/406712/85fd0ac5-d06d-3e9c-aacc-8e14dbcbdace.jpg) IE8/9 ![](http://dl.iteye.com/upload/attachment/406714/b2494717-e903-3cf7-b913-d03a3eacb614.jpg)

 

 

另见：[使用滤镜设置透明导致 IE 6/7/8/9  解析异常](/blog/875175)