通过 js 修改 html 的文本内容
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 DOCTYPE html>
2
3
4
5 xiao001
6
7
8 this is a js and html code
9 点击按钮将此处文本替换
10 点我
11
12
13 function my_function() {//替换demo里面的文本内容
14 document.getElementById("demo").innerHTML = "Hello javascript!";
15 }
16
17
18
19
View Code
同时我们可以发现,只要在对应 id 所在标签所包含的文本都会被替换,包括其下级标签包含的内容
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 DOCTYPE html>
2
3
4
5 xiao001
6
7
8 this is a js and html code
9
10
11 first
12 second
13 third
14
15 this will be replace too
16
17 点我
18
19
20 function my_function() {//替换demo里面的文本内容
21 document.getElementById("demo").innerHTML = "Hello javascript!";
22 }
23
24
25
26
View Code
通过 js 修改 html 样式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 DOCTYPE html>
2
3
4
5 xiao001
6
7
8 change the color of this paragraph
9 do it
10
11
12 function my_function() {
13 var cnt = document.getElementById('demo');//找到元素
14 cnt.style.color = "#ff0000";//改变样式
15 }
16
17
18
View Code
同理,只要在对应 id 所在标签所包含的样式都会做出对应变化,包括其下级标签中的样式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 DOCTYPE html>
2
3
4
5 xiao001
6
7
8
9 change the color of this paragraph
10
11 one
12 two
13 three
14
15
16 do it
17
18
19 function my_function() {
20 var cnt = document.getElementById('demo');//找到元素
21 cnt.style.color = "#ff0000";//改变样式
22 }
23
24
25
View Code
|