批改状态:合格
老师批语:本质上,jQuery就是一个js的全局函数对象而已, 不过在它上面定义了大量的实用的方法或属性,供用户操作, 来简化js编程
Composer: composer require components/jquery
CDN 与官网下载到本地的对比
<script src=""></script>引入$()函数jQuery()实现的,可简写为$()$()会返回一个 jQuery 对象,但它不是构造函数(不用new调用),而是工厂函数$()函数创建的对象,可以调用定义在 jQuery 对象上的所有方法和属性$()函数如此强大, 以至于不得不重点讨论一下,但在学习之前,应该将 jQuery 函数库引入到我们的项目中
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><script src="lib/jquery-3.5.1.js"></script><title>Document</title></head><body><ul id="first"><li>item1</li><li>item2</li><li>item3</li></ul><ul id="second"><li>item1</li><li>item2</li><li>item3</li></ul></body><script>// 1. $(选择器, 上下文): 返回jQuery对象// 改变li的文本颜色// 原生document.querySelectorAll("li").forEach(function (item) {item.style.color = "red";});// jQuery$("li").css("color", "blue");// 若只改变第一组的颜色$("li", "#first").css("color", "black");// 也可以这样写$("#first > li").css("color", "red");// 2. $(js对象), 返回一个jQuery对象, 将js对象包装成JQ对象// 给第二组加个背景颜色var lis = document.querySelectorAll("#second > li");// 原生lis.forEach(function (item) {item.style.backgroundColor = "yellow";});// 将lis包装成jq对象$(lis).css("background-color", "lightblue");// 3. $(html文本), 将html文本包装成一个jQuery对象并返回// 将标签插入到第一组中$("<h1>PHP中文网</h1>").insertBefore("#first");// 4. $(callback): 当html文档结构加载完成后就会立即执行这个回调$(function () {$("#first").css("background-color", "lightcyan");$("#second").css({"background-color": "#8888",color: "blue","font-size": "50px",});});</script></html>
jQuery()或$() + 用来创建 jQuery 对象 + 注册 DOM 就绪时需要执行的回调 + 充当 jQuery 命名空间 + 通常也称为jQuery全局对象,注意与 jQuery 对象的区别$('div').attr('style')$.each()
<script>var cl = console.log.bind(console);// 1. jQuery函数// $() === jQuery()// 改变body背景颜色// 原生document.querySelector("body").style.backgroundColor = "red";// jq对象$("body").css("backgroundColor", "yellow");jQuery("body").css("backgroundColor", "lightcyan");// 2.jQuery对象// 获取页面所有元素返回一个对象.对象无法进行foreach遍历cl($("*"));// 获取所有元素并将对象转为数组,进行遍历cl($("*").toArray().forEach(function (item) {cl(item);}));// 3.jQuery静态方法与方法// each() : 静态方法$.each($("*"), function (index, item) {cl(item); //序号 内容});// each()非静态方法$("*").each(function (index, item) {cl(index);});</script>
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><script src="lib/jquery-3.5.1.js"></script><title>查询结果的处理</title></head><body><ul><li>item1</li><li>item2</li><li>item3</li><li>item4</li><li>item5</li></ul></body></html>
toArray 将查询结果转为真正的数组
<script>var cl = console.log.bind(console);var lis = $("ul > li"); // 返回一个jq对象cl(lis);// 原生for (var i = 0; i < lis.length; i++) {// 改变字体颜色lis.get(i).style.color = "red";}// jQuerylis.toArray().forEach(function (item, index) {// 改变字体颜色$(item).css("color", "blue");if (index >= 2) {// 控制台打印cl("元素" + index + " : " + item);// 元素2 : [object HTMLLIElement]// 元素3 : [object HTMLLIElement]// 元素4 : [object HTMLLIElement]}});</script>
$.each() 回调的参数顺序与 forEach 不一样, \$().each(callback)
<script>var cl = console.log.bind(console);var lis = $("ul > li"); // 返回一个jq对象cl(lis);lis.each(function (index, item) {// 原生this.style.color = "red";// jq$(this).css("color", "blue");});</script>
$.map(), 必须要有返回值, 回调参数的参数与 foreach 的回调参数的参数相同
<script>var cl = console.log.bind(console);var lis = $("ul > li"); // 返回一个jq对象cl(lis);var arr = $.map(lis, function (item, index) {// 返回偶数if (index % 2) {return item;}});$(arr).css("color", "yellow");</script>
index(): 返回 jQuery 查询集合中的索引
<script>var cl = console.log.bind(console);var lis = $("ul > li"); // 返回一个jq对象cl(lis);lis.click(function () {// 打印下点击了谁cl("点击了", $(this).index() + 1);});</script>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><script src="lib/jquery-3.5.1.js"></script><title>Document</title><style>body {display: flex;flex-direction: column;align-items: center;color: #666;}form {width: 400px;/* height: 150px; */padding: 20px 10px;border: 1px solid #aaa;box-shadow: 0 0 5px #888;box-sizing: border-box;background-color: #eee;display: grid;grid-template-columns: 80px 200px;gap: 10px;place-content: center center;}button {grid-column: 2 / 3;height: 26px;}button:hover {color: white;background-color: #888;border: none;cursor: pointer;}.red {color: red;}</style></head><body><h2 class="red">用户登录</h2><form action="handle.php" method="get"><label for="email">Email:</label><inputtype="email"name="email"id="email"autofocusvalue="leture@php.cn"/><label for="password">Password:</label><input type="password" name="password" id="password" value="不少于6位" /><label for="confirm">记住我:</label><div><input type="radio" name="save" id="confirm" value="1" checked /><labelfor="confirm">保存</label><input type="radio" name="save" id="cancel" value="0" /><labelfor="cancel">放弃</label></div><button>登录</button></form></body><script>var cl = console.log.bind(console);// 拿到form;var form = $("form");// attr(name) : 获取 getter// attr(name,value) :修改 setter// 拿到action的值cl(form.attr("action")); //handle.php// 修改action的值form.attr("action", "1.php");cl(form.attr("action")); // 1.php// 修改多个值 使用对象字面量form.attr({action: "3.php",method: "post",}); //<form action="3.php" method="post">// 动态修改actionform.attr("action", function () {// 根据method值修改请求地址// 先把method值转为小写var method = $(this).attr("method").toLowerCase();return method === "get" ? "login.php" : "register.php";});</script></html>
attr(): 获取与设置 html 元素的属性removeAttr(): 移除 html 元素属性css(): 获取的是元素的计算样式,设置的是内联样式,但不适用于复合样式,如marginaddClass(): 添加类样式removeClass(): 移除类样式toggleClass(): 自动切换类样式hasClass(): 判断是否存在某个类样式val(): 获取和设置表单的值text(): 获取和设置元素的文本内容html(): 获取和设置元素的 html 内容offset(): 获取元素的位置信息scrollTop(),scrollLeft():获取元素滚动条位置data(): 获取和设置元素中的数据removeData(): 从元素中删除数据append() | appendTo(): 尾部插入元素prepend() | prependTo(): 头部插入元素after() | insertAfter(): 后面插入元素befor() | insertBefor(): 前面插入元素replaceWidh() | replaceAll(): 替换目标元素内容clone(): 创建并返回每一个选中元素的副本empty():remove():jQuery 简化了就是 js 代码,短短几行完成了原生几十行的功能,像是 php 的静态类 根据不同的参数完成不同的功能
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号