112_js笔记14_css的dom操作

一,静态直接修改标签的css样式

document.getElementById(id).style.property=新样式

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<p id="p1">Hello World!</p>
<p id="p2">Hello World!</p>
<script>
document.getElementById("p2").style.color="blue";
document.getElementById("p2").style.fontFamily="Arial";
document.getElementById("p2").style.fontSize="larger";
</script>
<p>以上段落通过脚本修改。</p>

</body>
</html>

浏览器运行结果:

 

二,动态触发修改标签的css样式

  • 1, 直接写在标签事件里边
<button type="button" onclick="document.getElementById('id1').style.color='red'">
点我!</button>
  • 2,写在js文件里边
 var btn1=document.getElementById('button');
 btn1.onclick=function(){
    btn1.style.color='red';
}

猜你喜欢

转载自blog.csdn.net/a_horse/article/details/84546007