前端开发学习Day22

238 阅读1分钟

第22天,依旧是做了3个案例,第一个是数组求和,第2个是弹出层效果,第3个是函数传参,改变Div任意属性。这3个案例中,我觉得最难的是数组求和,因为我之前没有接触过这个类型的案例,虽然照着源代码看了几遍,也百度了不会的知识点,但是依然没有搞明白,没办法,教我的人今天回家了,只能等五一回来之后再收拾这个案例了。通过第三个案例,我觉得函数真的挺强大的,这更加深了我要学精Js的念头。之前买的书也到了,期待之后的学习。以下是第三个案例的代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style type="text/css">
        body {
            text-align: center;
        }

        #box {
            width: 200px;
            height: 200px;
            background: black;
            color: white;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="p1">
        <p>属性名:<input type="text"></p>
    </div>
    <div class="p2">
        <p>属性值:<input type="text"></p>
    </div>
    <div><button>确定</button><button>重置</button></div>
    <div id="box">
        <p>在上方输入框输入"属性名""属性值",点击确定按钮查看效果。</p>
    </div>
    <script>
        var changeStyle = function (elem, name, value) {
            elem.style[name] = value
        }
        window.onload = function () {
            var oDiv = document.getElementById("box");
            var oBtn = document.getElementsByTagName("button");
            var oInput = document.getElementsByTagName("input");
            oBtn[0].onclick = function () {
                changeStyle(oDiv, oInput[0].value, oInput[1].value)
            };
            oBtn[1].onclick = function () {
                oDiv.removeAttribute("style")
            }
            
        }
    </script>
</body>

</html>

明天回家,带着电脑,争取学习不要落下。