块级作用域

2018-12-21  本文已影响0人  asmuzi
块级作用域
<!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>
    <script>
        if (12 > 5) {
            var a = 12;
        }

        console.log(a);
    </script>
</head>

<body>

</body>

</html>

块级作用域2
<!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>
    <script>
        window.onload = function () {
            let aBtn = document.getElementsByTagName('input');

            for (let i = 0; i < aBtn.length; i++) {
                aBtn[i].onclick = function () {
                    console.log(i);
                };
            }
        };
    </script>
</head>

<body>
    <div>
        <input type="button" value="a">
        <input type="button" value="b">
        <input type="button" value="c">
    </div>
</body>

</html>
上一篇 下一篇

猜你喜欢

热点阅读