初探jQuery

jQuery是什么

jQuery 是一个 JavaScript 库。jQuery 极大地简化了 JavaScript 编程。

使用jQuery 需要先进入jQuery文件。
有两种方式:

  1. 从官网下载后,通过script src属性加载

  2. 通过CDN加载

  1. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>

$():工厂函数

$():工厂函数,是全局函数,同时也是jQuery的别名,如果 $ 被占用可以用jQuery来操作。
$:也是构造函数,用来创建jQuery实例
jQuery对象:$()函数的返回值

  1. $("body").css("background","blue");
  2. jQuery("body").css("background","blue");

$():接收4种类型的参数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>$()函数</title>
  7. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  8. <style>
  9. /* #first li:last-of-type {
  10. } */
  11. </style>
  12. <script>
  13. // $(function(){
  14. // $("<li>这是回调函数生成的</li>").appendTo($("#first"));
  15. // })
  16. $(()=>$("<li>这是回调函数生成的</li>").appendTo($("#first")))
  17. </script>
  18. </head>
  19. <body>
  20. <ul id="first">
  21. <li>item1</li>
  22. <li>item2</li>
  23. <li>item3</li>
  24. </ul>
  25. <ul id="second">
  26. <li>item1</li>
  27. <li>item2</li>
  28. <li>item3</li>
  29. </ul>
  30. <script>
  31. //接收4种类型的参数
  32. // 1. $(selector,context):获取元素
  33. //原生
  34. //document.querySelectorAll("li").forEach(li => (li.style.color="red"));
  35. //jQuery
  36. // $("li").css("background","blue");
  37. $("li").css("color", "red");
  38. $("li", "#first").css("color", "blue");
  39. $("#first li").css("background", "green");
  40. //$("li","#first") === $("#first li") //并列写时,#first必须写在前面
  41. // 2. $(js对象):jQuery包装器,js对象是原生的js对象,将原生的js对象转为jQuery对象
  42. // 因为想使用jQuery对象上的非常丰富的方法或属性
  43. document.body.style.backgroundColor = "yellow";
  44. // 想用jQuery中的css方法来设置style属性(内联样式)
  45. $(document.body).css("background", "lightgreen");
  46. // 将jQuery对象还原成原生的js对象
  47. // console.log($(document.querySelectorAll("li")));
  48. // ...spread 扩展,...rest归并
  49. console.log([...$(document.querySelectorAll("li"))]);
  50. // [...$(document.querySelectorAll("li"))].forEach(
  51. // (li) => (li.style.backgroundColor = skyblue)
  52. // );
  53. // [...$(document.querySelectorAll("li"))].forEach(
  54. // (li) => (li.style.color = "skyblue")
  55. // );
  56. console.log($(document.querySelectorAll("li")).get(2));
  57. //get(n),[n] 可以获取jQuery集合中的第n个元素,返回原生对象
  58. $(document.querySelectorAll("li")).get(2).style.color = "yellow";
  59. $(document.querySelectorAll("li"))[1].style.color = "skyblue";
  60. // 3. $(html文本):生成dom元素
  61. // document.body.insertAdjacentHTML("afterend","<li>这是一个增加项</li>")
  62. document
  63. .querySelector("#first")
  64. .insertAdjacentHTML("beforeend", "<li>这是js生成的一个增加项</li>");
  65. // $("<li>这是jQuery生成的一个增加项</li>").appendTo(document.querySelector("#first"));
  66. $("<li>这也是jQuery生成的一个增加项</li>").appendTo($("#first"));
  67. // 4. $(callback回调函数):传一个回调函数当参数,当页面加载完成后会自动调用它
  68. //$() :参数的四种类型
  69. // 1. CSS选择器
  70. // 2. 原生js对象(包装器的功能)
  71. // 3. html字符串,创建dom元素
  72. // 4. 回调函数,在页面加载完成后,dom树创建成功后自动调用
  73. </script>
  74. </body>
  75. </html>

演示效果如图。
效果图

重要术语

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>重要术语</title>
  7. </head>
  8. <body>
  9. <!-- <script>
  10. function User(name,email) {
  11. this.name = name;
  12. this.email = email;
  13. this.get= function (){
  14. return this.name + ':'+this.email;
  15. };
  16. }
  17. User.prototype.hello = function (name) {
  18. return 'Hello ' + name;
  19. }
  20. User.say = function () {
  21. return ' static function ';
  22. }
  23. const obj = new User("admin","a@php.cn");
  24. //实例方法
  25. console.log(obj.get());
  26. //原型方法
  27. console.log(obj.hello('percy'));
  28. //类方法
  29. console.log(User.say());
  30. </script> -->
  31. <ul>
  32. <li>item1</li>
  33. <li>item2</li>
  34. <li>item3</li>
  35. <li>item4</li>
  36. <li>item5</li>
  37. </ul>
  38. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  39. <script>
  40. //页面中所有元素,只要经过$()包装,一定返回的是jQuery对象
  41. console.log($("*") instanceof jQuery);
  42. //只有jQuery对象才可以调用定义在jQuery上的方法。
  43. //静态方法,直接使用jQuery调用,jQuery想象成构造函数
  44. //function jQuery(){}
  45. // $.each($('li'),(key, item)=> console.log(key, item.textContent));
  46. // document.querySelectorAll('li').forEach((item,key)=>console.log(key,item.textContent));
  47. // $.each($('li'),(key, item)=>item.style.color = 'yellow');
  48. $.each($('li'),(key, item)=> $(item).css('color' , 'blue'));
  49. $.each($('li'),(key, item)=> $(item).css('border' , '1px solid red'));
  50. </script>
  51. </body>
  52. </html>

注意:一定要分别 原生的forEach(需要遍历的对象,callback(item,key)),和jQuery的each(需要遍历的对象,callback(key,item))参数的顺序不同。

第一组 getter/setter attr()

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>getter/setter1</title>
  7. <style>
  8. * {
  9. padding: 0;
  10. margin: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. display:flex;
  15. flex-direction: column;
  16. align-items: center;
  17. color:#666;
  18. }
  19. form {
  20. width: 400px;
  21. padding: 20px 10px;
  22. border:1px solid #aaa;
  23. box-shadow:0 0 5px #888;
  24. background-color: #eee;
  25. display:grid;
  26. grid-template-columns:80px 200px;
  27. gap:10px;
  28. place-content: center center;
  29. }
  30. button {
  31. grid-column: 2 / 3;
  32. height: 26px;
  33. }
  34. button:hover {
  35. color: white;
  36. background-color: #888;
  37. border:none;
  38. cursor:pointer;
  39. }
  40. .red {
  41. color:red;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <h2 class="red">用户登录</h2>
  47. <form action="handle.php" method="get">
  48. <label for="email">Email</label>
  49. <input
  50. type="email"
  51. name="email"
  52. id="emial"
  53. autofocus
  54. value="leture@php.cn"
  55. />
  56. <label for="password">Password</label>
  57. <input type="text" name="password" id="password" value="不少于6位" />
  58. <label for="confirm">记住我</label>
  59. <div>
  60. <input type="radio" name="save" id="confirm" value="1" checked />
  61. <label for="confirm">记住</label>
  62. <input type="radio" name="save" id="cancel" value="0" />
  63. <label for="cancel">放弃</label>
  64. </div>
  65. <button>登录</button>
  66. </form>
  67. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  68. <script>
  69. //attr():获取/设置元素属性
  70. //attr(name):getter
  71. //attr(name,value):setter
  72. const form = $("form");
  73. //action = "handle.php"
  74. console.log(form.attr("action"));
  75. form.attr("action","admin/check.php");
  76. console.log(form.attr("action"));
  77. //第一个参数通常是获取需要的属性,第二个参数使用回调
  78. form.attr("action",()=>{
  79. let method = form.attr('method').toUpperCase();
  80. return method === 'GET' ? 'query.php?id=2' : 'register.php';
  81. });
  82. console.log(form.attr("action"));
  83. </script>
  84. </body>
  85. </html>

第二组 getter/setter css()

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>getter/setter2</title>
  7. <style>
  8. * {
  9. padding: 0;
  10. margin: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. display: flex;
  15. flex-direction: column;
  16. align-items: center;
  17. color: #666;
  18. }
  19. form {
  20. width: 400px;
  21. padding: 20px 10px;
  22. border: 1px solid #aaa;
  23. box-shadow: 0 0 5px #888;
  24. background-color: #eee;
  25. display: grid;
  26. grid-template-columns: 80px 200px;
  27. gap: 10px;
  28. place-content: center center;
  29. }
  30. button {
  31. grid-column: 2 / 3;
  32. height: 26px;
  33. }
  34. button:hover {
  35. color: white;
  36. background-color: #888;
  37. border: none;
  38. cursor: pointer;
  39. }
  40. .red {
  41. color: red;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <h2 class="red">用户登录</h2>
  47. <form action="handle.php" method="get">
  48. <label for="email">Email</label>
  49. <input
  50. type="email"
  51. name="email"
  52. id="emial"
  53. autofocus
  54. value="leture@php.cn"
  55. />
  56. <label for="password">Password</label>
  57. <input type="text" name="password" id="password" value="不少于6位" />
  58. <label for="confirm">记住我</label>
  59. <div>
  60. <input type="radio" name="save" id="confirm" value="1" checked />
  61. <label for="confirm">记住</label>
  62. <input type="radio" name="save" id="cancel" value="0" />
  63. <label for="cancel">放弃</label>
  64. </div>
  65. <button>登录</button>
  66. </form>
  67. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  68. <script>
  69. //css():设置元素的行内样式
  70. //css(name):获取getter
  71. //css(name,value):setter;
  72. //css(name,callback):支持回调函数
  73. const form = $("form");
  74. //使用style只能获取到style属性上的行内/内联样式
  75. //css样式表的样式是拿不到的,必须使用计算样式才可以
  76. // console.log(document.querySelector("form").style.width);
  77. // console.log(window.getComputedStyle(document.querySelector("form"),null).getPropertyValue("width"));
  78. // jQuery 可以直接就拿到元素的最终的计算样式
  79. // console.log(form.css("width"));
  80. form.css("border-top", "6px solid blue");
  81. //css(obj):接收对象
  82. form.css({
  83. "border-bottom": "3px solid skyblue",
  84. "background-color": "lightcyan",
  85. });
  86. //css(属性或者对象,callback): 第二个参数支持回调函数
  87. form.css("background-color",()=>{
  88. const colors = ['plum','lightblue','lime','red'];
  89. const i = Math.floor(Math.random()*colors.length);
  90. return colors[i];
  91. })
  92. </script>
  93. </body>
  94. </html>

第三组 getter/setter val()

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>getter/setter3</title>
  7. <style>
  8. * {
  9. padding: 0;
  10. margin: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. display: flex;
  15. flex-direction: column;
  16. align-items: center;
  17. color: #666;
  18. }
  19. form {
  20. width: 400px;
  21. padding: 20px 10px;
  22. border: 1px solid #aaa;
  23. box-shadow: 0 0 5px #888;
  24. background-color: #eee;
  25. display: grid;
  26. grid-template-columns: 80px 200px;
  27. gap: 10px;
  28. place-content: center center;
  29. }
  30. button {
  31. grid-column: 2 / 3;
  32. height: 26px;
  33. }
  34. button:hover {
  35. color: white;
  36. background-color: #888;
  37. border: none;
  38. cursor: pointer;
  39. }
  40. .red {
  41. color: red;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <h2 class="red">用户登录</h2>
  47. <form action="handle.php" method="get" id="login">
  48. <label for="email">Email</label>
  49. <input
  50. type="email"
  51. name="email"
  52. id="email"
  53. autofocus
  54. value="leture@php.cn"
  55. />
  56. <label for="password">Password</label>
  57. <input type="password" name="password" id="password" value="不少于6位" />
  58. <label for="confirm">记住我</label>
  59. <div>
  60. <input type="radio" name="save" id="confirm" value="1" checked />
  61. <label for="confirm">记住</label>
  62. <input type="radio" name="save" id="cancel" value="0" />
  63. <label for="cancel">放弃</label>
  64. </div>
  65. <button>登录</button>
  66. </form>
  67. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  68. <script>
  69. // val():元素的值,表单控件的value属性
  70. console.log(document.forms["login"].email.value);
  71. console.log(document.forms.login.email.value);
  72. //jQuery
  73. console.log($("#email")[0].value);
  74. console.log($("#email").get(0).value);
  75. console.log($("#email").val());
  76. $("#email").val("admin@php.cn")
  77. console.log($("#email").val());
  78. console.log($("input:password").val());
  79. console.log($('input:radio[name="save"]:checked').val());
  80. console.log($('input:radio:checked').val());
  81. //val(callback):也支持回调函数
  82. $("email").val(()=> "xxx@qq.com");
  83. </script>
  84. </body>
  85. </html>

第四组 getter/setter html() ,text()

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>getter/setter4</title>
  7. <style>
  8. * {
  9. padding: 0;
  10. margin: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. display: flex;
  15. flex-direction: column;
  16. align-items: center;
  17. color: #666;
  18. }
  19. form {
  20. width: 400px;
  21. padding: 20px 10px;
  22. border: 1px solid #aaa;
  23. box-shadow: 0 0 5px #888;
  24. background-color: #eee;
  25. display: grid;
  26. grid-template-columns: 80px 200px;
  27. gap: 10px;
  28. place-content: center center;
  29. }
  30. button {
  31. grid-column: 2 / 3;
  32. height: 26px;
  33. }
  34. button:hover {
  35. color: white;
  36. background-color: #888;
  37. border: none;
  38. cursor: pointer;
  39. }
  40. .red {
  41. color: red;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <h2 class="red">用户<em style="color:green">登录</em> </h2>
  47. <form action="handle.php" method="get" id="login">
  48. <label for="email">Email</label>
  49. <input
  50. type="email"
  51. name="email"
  52. id="email"
  53. autofocus
  54. value="leture@php.cn"
  55. />
  56. <label for="password">Password</label>
  57. <input type="password" name="password" id="password" value="不少于6位" />
  58. <label for="confirm">记住我</label>
  59. <div>
  60. <input type="radio" name="save" id="confirm" value="1" checked />
  61. <label for="confirm">记住</label>
  62. <input type="radio" name="save" id="cancel" value="0" />
  63. <label for="cancel">放弃</label>
  64. </div>
  65. <button>登录</button>
  66. </form>
  67. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  68. <script>
  69. //html() :对应原生innerHTML
  70. //text() :对应原生innerText / textContent
  71. // const h2= document.querySelector("h2").innerHTML;
  72. // const h2= document.querySelector("h2").textContent;
  73. // console.log(h2);
  74. // console.log(document.querySelector("h2").innerHTML);
  75. //jQuery
  76. // 只获取纯文本
  77. // console.log($("h2").text());
  78. // 带有html的文本
  79. // console.log($("h2").html());
  80. $($("<h2><em>这是新增加的H2</em></h2>")).appendTo($("body"));
  81. </script>
  82. </body>
  83. </html>

总结:attr(),css(),val()
三个方法,一个参数时是获取,两个参数时是设置,第二个参数支持回调。这样就很强大。

html() :对应原生innerHTML
text() :对应原生innerText / textContent

原生forEach() 与 jQuery 的 each() 的区别

需要特别注意的是:要想使用jQuery的属性和方法,必须转为jQuery对象,哪怕只是原生js加$()包装器。另外当$被占用是可以使用 jQuery() 代替 $()

更多相关文章

  1. PHP基础知识:数组相关函数和操作
  2. 指向函数指针数组的指针
  3. 函数的定义和调用,函数的参数,函数的返回值,匿名函数(闭包)的申明和调
  4. PHP常用的数组函数
  5. 数组函数练习
  6. php数组的排序|数组的合并
  7. php学习笔记(数组函数_差集和交集的运算汇总)
  8. 小程序开发:调用百度文字识别接口实现图文识别,Node.js开发
  9. Spring、Shiro和EHCache 缓存的使用案例

随机推荐

  1. 前后端分离,最佳实践
  2. 服务端开发指南与最佳实战 | 数据存储技
  3. 非功能性需求,不要成为项目的坑
  4. 服务端开发指南与最佳实战 | 数据存储技
  5. 聊聊 MongoDB 使用场景
  6. 服务端开发指南与最佳实战 | 数据存储技
  7. 基于ssm框架实现的视频电影系统
  8. 微服务架构概述
  9. 服务端开发指南与最佳实战 | 数据存储技
  10. JavaScript测试教程-part 2:引入 Enzyme