01.jQuery getting started experience and native dom obtain the contrast element

jQuery third-party plug-ins, simple to use

demand:

Click the button to make the background color from pink box turns blue, becomes the width of 400px

1. Native 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>
        #box {
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <button id="btn">点击显示</button>
    <div id="box">
    </div>
    <script>
        window.onload = function(){
            var btn = document.getElementById("btn");
            btn.onclick = function(){
                var box = document.getElementById("box");
                box.style.backgroundColor = "skyblue";
          box.style.width = "400px";
            }
        }
        
    </script>
</body>
</html>

Renderings

     

 

 

 2.jQuery

Note: introducing jquery library

<!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>
        #box {
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
    <script src="./js/jquery.min.js"></script>
    <script>
        $(function () {
            $("#btn").click(function () {
                $("#box").css({
                    "backgroundColor": "skyblue",
                    "width": "400px"
                });
            })
        })
    </script>
</head>
<body>
    <button id="btn">点击显示</button>
    <div id="box">
    </div>
</body>
</html>

 Renderings

 

Guess you like

Origin www.cnblogs.com/z-ling/p/01jQuery.html