45 lines
819 B
Markdown
45 lines
819 B
Markdown
# dom编程
|
|
|
|
## 动态脚本
|
|
|
|
```javascript
|
|
// 创建script节点
|
|
var script = document.createElement("script")
|
|
// 设置src属性
|
|
script.src = "foo.js"
|
|
// 添加到body最后面
|
|
document.body.appendChild(script)
|
|
```
|
|
|
|
```javascript
|
|
var script = document.createElement("script")
|
|
script.appendChild(document.createTextNode("function hi(){alert('hi')};hi()"))
|
|
document.body.appendChild(script)
|
|
```
|
|
|
|
>.text
|
|
|
|
## 动态样式
|
|
|
|
```javascript
|
|
var style = document.createElement("style")
|
|
style.type = "text/css"
|
|
style.appendChild(document.createTextNode("body{background-color: red}"))
|
|
var head = document.getElementsByTagName("head")[0]
|
|
head.appendChild(style)
|
|
```
|
|
|
|
> styleSheet.cssText
|
|
|
|
## 操作表格
|
|
|
|
```javascript
|
|
var table = document.createElement("table")
|
|
table.border = 1
|
|
table.width="100%"
|
|
.....
|
|
// 自己操作
|
|
|
|
```
|
|
|