1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > JavaScript改变div的背景颜色

JavaScript改变div的背景颜色

时间:2018-09-18 02:04:09

相关推荐

JavaScript改变div的背景颜色

最简便的写法<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>任务三</title><script type="text/javascript">function bs(v) {kuang.style.backgroundColor=v;}</script></head><body><p>请选择颜色:</p><select id="test" name="xuanzekuang" οnchange="bs(this.value)"><option value="red">红</option><option value="green">绿</option><option value="blue">蓝</option><option value="black">黑</option></select><div id="kuang" style="width: 100px;height: 100px;border: 1px solid #eee"></div></body></html>

第二简便写法<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>任务三</title><script type="text/javascript">function bs() {var myselect = document.getElementById("test");var index = myselect.selectedIndex;var zhi = myselect.options[index].value;var kuang = document.getElementById("kuang");kuang.style.backgroundColor=zhi;}</script></head><body><p>请选择颜色:</p><select id="test" name="xuanzekuang" οnchange="bs()"><option value="red">红</option><option value="green">绿</option><option value="blue">蓝</option><option value="black">黑</option></select><div id="kuang" style="width: 100px;height: 100px;border: 1px solid #eee"></div></body></html>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。