01.jQuery入门体验和原生dom获取元素的对比
jQuery第三方插件,简洁易用
需求:
点击按钮让盒子背景颜色从粉色变蓝色,宽度变为400px
1.原生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>
效果图
2.jQuery
注意:引入jquery库
<!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>
效果图