通过 js 修改 html 的文本内容或者样式

您所在的位置:网站首页 javascript修改文本 通过 js 修改 html 的文本内容或者样式

通过 js 修改 html 的文本内容或者样式

#通过 js 修改 html 的文本内容或者样式| 来源: 网络整理| 查看: 265

通过 js 修改 html 的文本内容 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 所在标签所包含的文本都会被替换,包括其下级标签包含的内容

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 样式 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 所在标签所包含的样式都会做出对应变化,包括其下级标签中的样式

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

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3