Blogger Information
Blog 55
fans 3
comment 0
visits 54979
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
初学jQuery
王佳祥
Original
700 people have browsed it

初学jQuery

1.$()的用法

  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>Document</title>
  7. <!-- <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.5.0.min.js"></script> -->
  8. <script src="js/jquery-3.5.1.min.js"></script>
  9. </head>
  10. <body>
  11. <h1>今天学jQuery</h1>
  12. <div class="one">
  13. <ul>
  14. <li>item1</li>
  15. <li>item2</li>
  16. <li>item3</li>
  17. </ul>
  18. </div>
  19. <div class="two">
  20. <ul>
  21. <li>item1</li>
  22. <li>item2</li>
  23. <li>item3</li>
  24. </ul>
  25. </div>
  26. <script>
  27. //1.$():工厂函数
  28. $("h1").css("color", "red");
  29. //$()的第二个参数,上下文
  30. $("li", ".one").css("color", "green");
  31. $("li", ".two").css("color", "blue");
  32. //2.$(js原生对象)
  33. //将js对象包装成一个jquery对象,从而可以直接调用jQuery中的方法
  34. var lis = document.querySelectorAll(".two li");
  35. console.log(lis);
  36. lis.forEach(function (item) {
  37. item.style.background = "yellow";
  38. });
  39. //想使用jquery中的css()方法,必须将lis包装/转换成一个jQuery对象
  40. console.log($(lis));
  41. $(lis).css("background-color", "pink");
  42. //3.$(html文本) 创建元素
  43. /* var h2 = document.createElement("h2");
  44. h2.innerText = "文本内容";
  45. document.body.appendChild(h2); */
  46. $("<h2>文本内容</h2>").appendTo(document.body);
  47. //4.$(callback):当html文档加载完之后立即调用这个函数
  48. $(function () {
  49. $(document.body).css({
  50. background: "wheat",
  51. "font-size": "20px",
  52. });
  53. });
  54. </script>
  55. </body>
  56. </html>


2.重要术语

  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="js/jquery-3.5.1.min.js"></script>
  8. </head>
  9. <body>
  10. <script>
  11. //1.jquery函数
  12. $(document.body).css("background", "wheat");
  13. jQuery(document.body).css("background", "green");
  14. document.body.style.backgroundColor = "lightgreen";
  15. //2.jQuery对象
  16. console.log($("*"));
  17. console.log($("*").length);
  18. console.log($("*")[6]);
  19. $("*")[6].style.backgroundColor = "lightblue";
  20. $("*").get(6).style.backgroundColor = "lightgreen";
  21. //$("*")[6] === $("*").get(6)
  22. //3.jQuery静态方法
  23. //$:函数,函数对象
  24. //$():jQuery对象
  25. /* $.each($("*"), function (key, value) {
  26. console.log(key, value);
  27. }); */
  28. //each()此时就不是静态方法了,为什么,它在对象上调用
  29. $("*").each(function (key, value) {
  30. console.log(key, value);
  31. });
  32. </script>
  33. </body>
  34. </html>


3.查询结果的处理

  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="js/jquery-3.5.1.min.js"></script>
  8. </head>
  9. <body>
  10. <ul>
  11. <li>item1</li>
  12. <li>item2</li>
  13. <li>item3</li>
  14. <li>item4</li>
  15. <li>item5</li>
  16. </ul>
  17. <script>
  18. //1.toArray():将结果转为真正数组
  19. console.log(document.getElementsByTagName("li"));
  20. //返回一个集合
  21. var lis = $(document.getElementsByTagName("li")).toArray();
  22. console.log(lis);
  23. lis.forEach(function (item) {
  24. item.style.color = "red";
  25. });
  26. //2.map()
  27. //map() 把每个元素通过函数传递到当前匹配集合中,生成包含返回值的新的 jQuery 对象
  28. var arr = [1, 2, 3, 4];
  29. var res = arr.map(function (item) {
  30. return item * 2;
  31. });
  32. console.log(arr, res);
  33. var res2 = $.map(arr, function (item) {
  34. return item * 2;
  35. });
  36. console.log(res2);
  37. //3.index():返回jQuery集合中的元素的索引
  38. //jquery对象是一个类数组:具有从0开始的递增的正整数索引,并且还有一个length属性
  39. var obj = {
  40. 0: "a",
  41. 1: "b",
  42. 2: "c",
  43. 3: "d",
  44. length: 4,
  45. };
  46. //obj满足类数组特征,所以它就是类数组(类似一个数组)
  47. console.log($("li"));
  48. $("li").click(function () {
  49. console.log($(this).index() + 1);
  50. });
  51. </script>
  52. </body>
  53. </html>


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>getter/setter</title>
  7. </head>
  8. <body>
  9. <h2 class="red">用户登录</h2>
  10. <form action="handle.php" method="POST">
  11. <label for="username">账号:</label>
  12. <input
  13. type="text"
  14. name="username"
  15. id="username"
  16. placeholder="请输入账号!"
  17. required
  18. />
  19. <label for="">密码:</label>
  20. <input
  21. type="password"
  22. name="password"
  23. id="password"
  24. placeholder="请输入密码!"
  25. required
  26. />
  27. <label for="">记住我</label>
  28. <div>
  29. <input type="radio" name="save" id="confrim" value="1" checked />
  30. <label for="confrim">保存</label>
  31. <input type="radio" name="save" id="cancel" value="0" />
  32. <label for="cancel">放弃</label>
  33. </div>
  34. <button>登录</button>
  35. </form>
  36. <script src="js/jquery-3.5.1.min.js"></script>
  37. <script>
  38. var form = $("form");
  39. //attr():获取元素的属性
  40. //attr(name):获取属性的值
  41. //attr(name,value):设置属性的值
  42. console.log(form.attr("action"));
  43. //设置属性
  44. form.attr("action", "admin/check.php");
  45. console.log(form.attr("action"));
  46. //同时设置多个属性
  47. form.attr({
  48. action: "index.php",
  49. method: "GET",
  50. });
  51. console.log(form.attr("action"));
  52. console.log(form.attr("method"));
  53. //attr()第二个参数支持回调
  54. //根据请求的类型,动态设置action
  55. //get ==> query.php?id=1
  56. //post ==> register.php
  57. form.attr("action", function () {
  58. var method = $(this).attr("method");
  59. console.log(method);
  60. return method === "get" ? "query.php?id=1" : "register.php";
  61. });
  62. </script>
  63. </body>
  64. </html>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post