jQuery——开关灯

js对象与jquery对象的相互转化:

1、$(js对象)

2、$(selector).get(索引)、$(selector)[索引]

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div {
            width: 700px;
            height: 600px;
            margin: 50px auto;
            background-color: blue;
        }
    </style>
    <script src="jquery-1.11.1.js"></script>
    <script>
        // $(document).ready(function () {
        //     $("button:eq(0)").click(function () {
        //         $("body").css("backgroundColor", "");
        //     });
        //     $("button:eq(1)").click(function () {
        //         $("body").css("backgroundColor", "black");
        //     });
        // });

        $(function () {
            var btnArr = document.getElementsByTagName("button");
            $(btnArr[0]).click(function () {
                $("body").get(0).style.backgroundColor = "";

            });
            $(btnArr[1]).click(function () {
                $("body")[0].style.backgroundColor = "black";

            });
        });
    </script>
</head>
<body>
<button>开灯</button>
<button>关灯</button>
<div></div>
</body>
</html>

原文地址:https://www.cnblogs.com/wuqiuxue/p/8031363.html