<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>函数传参应用</title> </head> <style> #root{ width: 100px; height: 100px; background: red; } </style> <body> <input type="button" value="变绿" onclick="setStyle(\'background\',\'green\')"/> <input type="button" value="变宽" onclick="setStyle(\'width\',\'200px\')"/> <input type="button" value="变高" onclick="setStyle(\'height\',\'400px\')"/> <div id="root"></div> </body> <script> function setStyle(name,value) { var root =document.getElementById(\'root\'); root.style[name]=value; } </script> </html>
效果图: