JavaScript的第二个作业

  今天的作业用到函数传参,需要注意的一点是,区分变量和字符串的概念,简单来说,就是分清楚那些要加引号,那些不需要。(哭。。。我搞的不是很清楚两者之间的区别)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            #on{width: 100px; height: 100px;background-color:green;border:1px #008000 dashed;}
        </style>
        <script>
            function setstyle(name,value)
            {
                var odiv=document.getElementById('on');
                
                odiv.style[name]=value;
            }
        </script>
    </head>
    <body>
        <input type="button" value="变高" onclick="setstyle('height','1000px')"/>
        <input type="button" value="变宽" onclick="setstyle('width','1000px')"/>
        <input type="button" value="变色" onclick="setstyle('background-color','red')"/>
        <div id="on"></div>
    </body>
</html>

如果用昨天的方法来做的话,需要定义三个函数给三个按钮,函数传参让这个问题变得很简单。

原文地址:https://www.cnblogs.com/sishiuliunian/p/4875349.html