当前位置:精东方网络知识网 >> 网站建设 >> div >> 详情

js如何设置div的属性

可以使用JavaScript的属性设置方法来设置div的属性。

1. 使用`setAttribute`方法:

```javascript

div.setAttribute('属性名', '属性值');

```

2. 直接给属性赋值:

```javascript

div.属性名 = '属性值';

```

例如,设置一个div的背景颜色为红色:

```javascript

// 使用setAttribute方法

div.setAttribute('style', 'background-color: red;');

// 直接赋值

div.style.backgroundColor = 'red';

```

标签:div