博客列表 >jQuery常用方法和DOM操作

jQuery常用方法和DOM操作

phpcn_u202398
phpcn_u202398原创
2020年06月01日 09:29:011062浏览

1、getter和setter

  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="jquery-3.4.1.js"></script>
  8. <style>
  9. body{
  10. display: flex;
  11. flex-direction: column;
  12. align-items: center;
  13. }
  14. form{
  15. width: 350px;
  16. box-sizing: border-box;
  17. padding: 20px 8px;
  18. display: grid;
  19. grid-template-columns: 70px 180px;
  20. gap: 5px;
  21. place-content: center center;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <form action="" method="POST">
  27. <label for="">用户名:</label>
  28. <input type="email" name="email" id="email" value="zzz@qq.com">
  29. <label for="">密码:</label>
  30. <input type="password" name="pwd" id="" value="****">
  31. <label for="">记住我:</label>
  32. <div>
  33. <input type="radio" name="jw" id="" value="1" checked>保存
  34. <input type="radio" name="jw" id="" value="0">放弃
  35. </div>
  36. </form>
  37. <h3></h3>
  38. </body>
  39. </html>
  40. <script>
  41. var lg = console.log.bind(console);
  42. var form = $("form");
  43. // 1. attr(): html属性进行操作
  44. // attr(name):  getter
  45. // attr(name, value): setter
  46. lg(form.attr("action"));
  47. form.attr("action","inof.php");
  48. lg(form.attr("action"));
  49. form.attr("action",function(){
  50. var method = $(this).attr("method").toLowerCase();
  51. return method === "get" ? "ind.php?id=3" : "infon.php";
  52. });
  53. //2. css(): 针对 html元素的style属性进行操作
  54. form.css("border", "2px solid #698aab");
  55. // 3. val():表单元素的值
  56. $("#email").val("za@qq.com")
  57. //lg($("#email").val());
  58. // 4. html()/text(): 元素内容操作
  59. document.querySelector("h3").innerText = "请核对自己的登录信息"
  60. $("h3").css("color","red");
  61. </script>


2、DOM操作

  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. <script src="jquery-3.4.1.js"></script>
  7. <title>DOM</title>
  8. <style>
  9. .active {
  10. color: red;
  11. }
  12. </style>
  13. </head>
  14. <body></body>
  15. </html>
  16. <script>
  17. var cl = console.log.bind(console);
  18. // 1. 元素的插入与替换, 父元素.append(子元素)
  19. $("body").append("<ol>");
  20. // 子元素.appendTo(父元素)
  21. $("<li>").text("电器").appendTo("ol");
  22. $("<li>").addClass("active").text("衣服").appendTo("ol");
  23. $("<li>", {
  24. id: "hello",
  25. style: "background-color:yellow",
  26. })
  27. .html("<a href=''>日用品</a>")
  28. .appendTo("ol");
  29. $("li:first-of-type").append("<ol>")
  30. // append(callback)
  31. $("li:first-of-type > ol").append(function () {
  32. var res = "";
  33. for (var i = 0; i < 5; i++) {
  34. res += "<li><a href=''>最新商品" + (i + 1) + "</a></li>";
  35. }
  36. return res;
  37. });
  38. // prepend(), prependTo(), 将新元素插入到头部
  39. $("<li>最新留言</li>").prependTo("ol:first-of-type");
  40. // 元素的换: replaceWith()
  41. $("ol > li:last-of-type").replaceWith("<h3>Hello PHP.CN</h3>");
  42. //$("<p>Hello World...</p>").replaceAll("ol > li:first-of-type");
  43. </script>


学习总结

本节课我们学习了jQuery的常用方法——fetter与setter和DOM操作,通过本节课的学习知道了如何通过fetter和setter来操作html元素的属性,知道了DOM操作。方便以后再实战中的应用。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议