1.$()的四种类型参数的应用场景实例演示

案例 html 结构:

  1. <ul class="list">
  2. <li class="item">item1</li>
  3. <li class="item">item2</li>
  4. <li class="item">item3</li>
  5. <li class="item">item4</li>
  6. <li class="item">item5</li>
  7. </ul>

1. $(选择器, 上下文)

  1. // 1. $(选择器, 上下文): 获取dom元素
  2. // 将所有li前景色更新成红色
  3. // 原生
  4. document.querySelectorAll(".list .item").forEach((item) => (item.style.color = "red"));
  5. // jQuery
  6. console.log($(".item", ".list"));
  7. $(".item", ".list").css("color", "green");
  8. // 第二参数上下文完全可以用第一个参数来模拟
  9. $(".list .item").css("color", "blue");

2. $(js 原生对象)

  1. // 2. $(js原生对象): 将原生js对象转换jQuery对象,也叫"包装"成jQuery对象
  2. // 这时,这个$()也有一个名称: 包装器
  3. // document.body.css("background-color", "yellow");
  4. $(document.body).css("background-color", "yellow");
  5. console.log($(document.body) instanceof jQuery);

3. $(html 文本)

  1. // 3. $(html文本): 创建dom元素,直接包装成jQuery对象返回
  2. // const p = document.createElement("p");
  3. // p.textContent = "Hello World";
  4. // document.body.prepend(p);
  5. document.body.insertAdjacentHTML("afterbegin", "<p>哈哈,我还没有吃晚饭</p>");
  6. $("<p>同志们,辛苦了</p>").insertAfter(".list");

4. $(回调)

  1. // 4. $(回调): dom树一旦创建完成,就会立即执行这个回调
  2. // $("body h2").css("color", "red");
  3. $(() => {
  4. $("body h2").css("color", "red");
  5. });

2. 将课堂提及的所有 jQuery 方法全部实例演示

案例 html 结构:

  1. <body id="main">
  2. <h2 class="title">用户登录</h2>
  3. <form action="handle.php" method="POST" id="login">
  4. <label for="email">Email:</label>
  5. <input type="email" name="email" id="email" autofocus placeholder="leture@php.cn" />
  6. <label for="password">Password:</label>
  7. <input type="password" name="password" id="password" placeholder="不少于6位" />
  8. <label for="confirm">记住我:</label>
  9. <div>
  10. <input type="radio" name="save" id="confirm" value="1" checked />
  11. <label for="confirm">保存</label>
  12. <input type="radio" name="save" id="cancel" value="0" />
  13. <label for="cancel">放弃</label>
  14. </div>
  15. <button>登录</button>
  16. </form>
  17. </body>

案例 css 样式:

  1. body {
  2. display: flex;
  3. flex-direction: column;
  4. align-items: center;
  5. background-color: lightcyan;
  6. font-weight: lighter;
  7. }
  8. #login {
  9. width: 400px;
  10. padding: 20px 10px;
  11. border: 1px solid #aaa;
  12. box-shadow: 0 0 5px #888;
  13. box-sizing: border-box;
  14. background-color: lightseagreen;
  15. color: #fff;
  16. display: grid;
  17. grid-template-columns: 80px 200px;
  18. gap: 10px;
  19. place-content: center center;
  20. }
  21. #login input {
  22. border: none;
  23. outline: none;
  24. }
  25. button {
  26. grid-column: 2 / 3;
  27. height: 32px;
  28. background-color: lightskyblue;
  29. border: none;
  30. outline: none;
  31. }
  32. button:hover {
  33. color: white;
  34. background-color: coral;
  35. color: #fff;
  36. /* border: none; */
  37. cursor: pointer;
  38. }
  39. .title {
  40. color: #666;
  41. font-weight: lighter;
  42. }

1. 属性操作: attr()

  1. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
  2. <script>
  3. // console.log($("body").attr("id"));
  4. // attr():获取/设置元素属性
  5. // attr(name):getter
  6. // attr(name,value): setter
  7. // removeAttr(name): 删除属性
  8. const form = $("form");
  9. // 获取
  10. console.log(form.attr("action"));
  11. // 设置
  12. form.attr("action", "admin/check.php");
  13. console.log(form.attr("action"));
  14. // 第二个参数允许使用回调
  15. // 根据表单的请求类型, 动态设置不同的处理脚本
  16. // get: action = query.php?id=2
  17. // post: action = register.php
  18. form.attr("action", () => {
  19. return form.attr("method").toLocaleLowerCase() === "post" ? "register.php" : "query.php?id=2";
  20. });
  21. console.log(form.attr("action"));
  22. // 删除属性
  23. $("h2").removeAttr("class");
  24. </script>

2.行内样式操作: css()

  1. // css(): 包括了行内样式的计算样式
  2. // css(name): getter
  3. // css(name,value): setter
  4. // css(name,callback)
  5. // 原生
  6. console.log(document.querySelector("form").style.width);
  7. console.log(window.getComputedStyle(document.querySelector("form"), null).getPropertyValue("width"));
  8. // jquery
  9. const form = $("#login");
  10. // css()可以直接获取到计算样式
  11. console.log(form.css("width"));
  12. form.css("border-top", "5px solid yellow");
  13. // css(obj), 同时设置多个样式声明
  14. form.css({
  15. "border-bottom": "5px solid #000",
  16. background: "green",
  17. });
  18. // css(name,callback)
  19. // 表单的背景色随机变换
  20. form.css("background", () => {
  21. const colors = ["lightpink", "lightblue", "lime", "wheat"];
  22. // 生成一个随机的数组下标, 0 - 3之间
  23. return colors[Math.floor(Math.random() * colors.length)];
  24. });

3.表单控件的值: val()

  1. // val():无参,默认就是获取控件元素(input,select...)的value属性的值
  2. // val(param): 设置
  3. // val(callback)
  4. // 原生
  5. document.forms.login.email.value = "admin@qq.com";
  6. console.log(document.forms.login.email.value);
  7. // jquery
  8. console.log($("#email").val());
  9. // $("#password").val("123456");
  10. $("input:password").val("123456");
  11. console.log($("input:password").val());
  12. console.log($("input:radio:checked").val());
  13. $("#email").val(() => "朱老师@php.cn");

4. text(), html()

  1. <div class="box"></div>
  2. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
  3. <script>
  4. // text(): 读/写入文本, textContent
  5. // html(): 读/写html文本, innerHTML
  6. $(".box").text("hello world");
  7. // text()不能解析文本中的html,转为实体字符原样输出
  8. $(".box").text("<strong style='color:red'>hello world</strong>");
  9. // html()
  10. $(".box").html("<strong style='color:red'>hello world</strong>");
  11. </script>

5. class 操作

  1. <style>
  2. .title {
  3. color: red;
  4. }
  5. </style>
  6. <h1>php.cn</h1>
  7. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
  8. <script>
  9. // 原生: classList对象
  10. let h1 = document.querySelector("h1");
  11. h1.classList.add("title"); // 添加class
  12. h1.classList.remove("title"); // 移除class
  13. h1.classList.toggle("title"); // 切换class
  14. // -------------------------------
  15. // jquery
  16. // addClass() => classList.add()
  17. // removeClass() => classList.remove()
  18. // toggleClass() => classList.toggle()
  19. const h1 = $("h1");
  20. h1.addClass("title");
  21. h1.removeClass("title");
  22. h1.toggleClass("title");
  23. </script>

3. 为什么需要将 jQuery 对象转 js 对象,方法是什么?

因为 jQuery 的局限性,很多场景下需要将 jQuery 对象转为 js 原生对象来调用 js 功能完成操作

  1. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
  2. <ul class="list">
  3. <li class="item">item1</li>
  4. <li class="item">item2</li>
  5. <li class="item">item3</li>
  6. <li class="item">item4</li>
  7. <li class="item">item5</li>
  8. </ul>
  9. <script>
  10. // 因为jQuery的局限性,很多场景下需要将jQuery对象转为js原生对象来调用js功能完成操作
  11. console.log($(".list .item"));
  12. // 任何一个$()返回的都是一个jQuery集合对象
  13. // 整体集合是一个JQuery对象,但是集合中的每个成员却是原生的js对象
  14. console.log($(".list .item")[0]);
  15. // 第一个li本身就是原生js对象
  16. $(".list .item")[0].style.backgroundColor = "yellow";
  17. // 也可以使用jQuery封装的另一个方法
  18. $(".list .item").get(2).style.backgroundColor = "lightgreen";
  19. // 将整个ul当成js原生对象
  20. console.log($(".list"));
  21. console.log($(".list")[0]);
  22. $(".list")[0].style.border = "2px solid";
  23. </script>

更多相关文章

  1. BlueStore源码分析之对象IO
  2. spark 类对象去重
  3. 一文读懂java中的Reference和引用类型
  4. jQuery入门学习一
  5. 面向对象系列教材 (三)- Java类的方法
  6. 面向对象系列教材 (二)- Java类的属性
  7. 面向对象系列教材 (一)- Java中的类和对象
  8. 反射机制
  9. vue之elementui,树形组件el-tree实现左右拖动滚动条

随机推荐

  1. 将对象值传递给指令而不是任何其他数据变
  2. 使用angularjs添加包含事件的新标记
  3. 使用倒计时进度条退出
  4. jQuery Slimbox没有正确请求文件
  5. 如何通过javascript显示隐藏的div?
  6. Javascript对象
  7. 在jQuery中使用css transform属性
  8. 根据用户时区显示当地时间 php+javascrip
  9. 从两个数组生成JSON
  10. 使用谷歌地图computeDistanceBetween获取