函数传参一:
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style type="text/css"> #div1 { width: 100px; height: 100px; background:red; } </style> <script> function setcolor(color) { var odiv=document.getElementById('div1'); odiv.style.background=color; } </script> </head> <body id="div1"> <input type="button" value="黄色" onClick="setcolor('yellow')" /> <input type="button" value="绿色" onClick="setcolor('green')" /> </body> </html>
函数传参二:
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style type="text/css"> #div1 { width: 100px; height: 100px; background: red; } </style> <script> function setstyle(a,b) { var odiv=document.getElementById('div1'); odiv.style[a]=b; } </script> </head> <body> <input type="button" value="变宽" onClick="setstyle('width','300px')" /> <input type="button" value="变高" onClick="setstyle('height','300px')" /> <input type="button" value="变色" onClick="setstyle('background','green')" /> <div id="div1"></div> </body> </html>