赞
踩
错误的写法
<!doctype html> <html> <head> <meta charset="utf-8"> <title>根据下拉框选中的值修改背景颜色</title> <script type="text/javascript"> function changecolo(colo){//要有返回值 var color=colo.value; alert(color) if(color="red"){ document.bgcolor="#FF0000" } if(color="yellow"){ document.bgcolor="#FFFF00" } if(color="green"){ document.bgcolor="#00FF00" } if(color="blue"){ document.bgcolor="#0000FF" } else{ document.bgcolor="#FFFFFF" } } </script> </head> <body> <form name="form1"> <select id="colo" onChange="changecolo(this)"> <option value="red">红色</option> <option value="yellow">黄色</option> <option value="green">绿色</option> <option value="blue">蓝色</option> </select> </form> </body> </html>
纠正后
专门建立的学习Q-q-u-n: 731771211,分享学习方法和需要注意的小细节,不停更新最新的教程和学习技巧 (从零基础开始到前端项目实战教程,学习工具,全栈开发学习路线以及规划 <!doctype html> <html> <head> <meta charset="utf-8"> <title>根据下拉框选中的值修改背景颜色</title> <script type="text/javascript"> function changecolor(colo){ var color=colo.value alert(color) document.bgColor=color } </script> </head> <body> <form name="form1"> <select id="colo" onChange="changecolor(this)"> <option value="red">红色</option> <option value="yellow">黄色</option> <option value="green">绿色</option> <option value="blue">蓝色</option> </select> </form> </body> </html>
或者用document.getElementById()做
<!doctype html> <html> <head> <meta charset="utf-8"> <title>根据下拉框选中的值修改背景颜色</title> <script type="text/javascript"> function changecolor(){ var color=document.getElementById('colo').value alert(color) document.bgColor=color } </script> </head> <body> <form name="form1"> <select id="colo" onChange="changecolor()"> <option value="red">红色</option> <option value="yellow">黄色</option> <option value="green">绿色</option> <option value="blue">蓝色</option> </select> </form> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。