JavaScript修改元素样式

原创 2019-01-28 14:00:49 291
摘要:<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">&

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

</head>

<body>

<button onclick="f1(this)">变高</button>

<button onclick="f2(this)">变宽</button>

<button onclick="f3(this)">变色</button>

<button onclick="f4(this)">重置</button>

<button onclick="f5(this)">隐藏</button>

<button onclick="f6(this)">显示</button>

<div id="box" style="width:100px;height:100px;background-color:blue;margin:50px 100px;"></div>

<script>

var btn=document.getElementById('box');

function f1(){btn.style.height="200px";    }

function f2(){btn.style.width="200px";    }

function f3(){btn.style.background="red";    }

function f4(){btn.style.height="100px";btn.style.width="100px";btn.style.background="blue";}

function f5(){btn.style.display="none";    }

function f6(){btn.style.display="block";    }

</script>

</body>

</html>

批改老师:天蓬老师批改时间:2019-01-28 14:38:53
老师总结:function f1(){btn.style.height="200px"; }如果只有一行代码,不如直接写到html属性中

发布手记

热门词条