博客列表 >演示$()函数的四个参数以及部分getter/setter方法

演示$()函数的四个参数以及部分getter/setter方法

lus菜
lus菜原创
2021年01月14日 22:20:48561浏览

$()函数:

  1. $(选择器,上下文): 获取元素
  2. $(js对象): jQuiey包装器,js对象是原生的js对象,将原生的js对象转为jQuery对象
  3. $(html文本): 生成dom元素
  4. $(callback回调函数):传一个回调当参数,当页面加载完成后会自动调用它

样式代码:

  1. <title>$()</title>
  2. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  3. <script>
  4. $(function () { // 回调函数
  5. $("<li>今天要晚起</li>").appendTo(document.querySelector("#first"));
  6. console.log(document.querySelector("#first"));
  7. });
  8. </script>
  9. <body>
  10. <ul id="first">
  11. <li>item1</li>
  12. <li>item2</li>
  13. <li>item3</li>
  14. </ul>
  15. <ul id="second">
  16. <li>item1</li>
  17. <li>item2</li>
  18. <li>item3</li>
  19. </ul>
  20. <script>
  21. //$(选择器,上下文): 获取元素 原生
  22. document.querySelectorAll("li").forEach((li) => (li.style.color = "blue"));
  23. $("#first li").css("color", "green"); // jquery
  24. document.body.style.backgroundColoe = "yellow"; // 因为想使用jQuery对象上的非常丰富的方法或属性
  25. $(document.body).css("background", "lightgreen"); //想用jquery中的css方法来设置style属性(内联样式)
  26. //将jquery对象还原成原生的js对象集合 ....spread扩展,....rest归并
  27. console.log($(document.querySelectorAll("li")).get(2));
  28. // get(n),[n]: 将jQuery集合中的某一个对象还原成原生的js对象
  29. $(document.querySelectorAll("li")).get(2).style.backgroundColor =
  30. "violet";
  31. $(document.querySelectorAll("li"))[0].style.backgroundColor = "white";
  32. document //$(html文本): 生成dom元素
  33. .querySelector("#first")
  34. .insertAdjacentHTML("beforeend", "<li>大家晚上好呀~~</li>");
  35. console.log($("<li>大家晚上好呀~~</li>"));
  36. $("<li>今天吃了吗?</li>").appendTo(document.querySelector("#first"));
  37. </script>
  38. </body>

效果预览:

getter/setter方法:

样式代码:

  1. <body>
  2. <h2 class="red">用户登录</h2>
  3. <form action="handle.php" method="post">
  4. <label for="email">账号:</label>
  5. <input type="email" name="email" id="email" autofocus value="leture@php.cn" />
  6. <label for="password">密码:</label>
  7. <input type="password" name="password" id="password" value="不少于六位" />
  8. <label for="confirm">记住密码:</label>
  9. <div>
  10. <input type="radio" name="save" id="confirm" value="1" checkeb /><label for="confirm">保存</label>
  11. <input type="radio" name="save" id="cancel" value="0" /><labelfor="cancel">返回</label>
  12. </div>
  13. <button>登录</button>
  14. </form>
  15. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  16. <script>
  17. // attr():获取/设置元素属性
  18. // attr(name):getter / attr(name,value): setter
  19. const form = $("form");
  20. console.log(form.attr("action"));
  21. form.attr("action", "admin/check.php");
  22. console.log(form.attr("action"));
  23. // 第二个参数使用回调
  24. form.attr("action", () => {
  25. let method = form.attr("method").toUpperCase();
  26. return method === "GET" ? "query.php?id=2" : "register.php";
  27. });
  28. console.log(form.attr("action"));
  29. </script>
  30. </body>
  31. </html>

css代码:

  1. <style>
  2. body {
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. color: violet;
  7. }
  8. form {
  9. width: 400px;
  10. /* height: 150px; */
  11. padding: 20px 10px;
  12. border: 1px solid blue;
  13. box-shadow: 0 0 5px blueviolet;
  14. box-sizing: border-box;
  15. background-color: #eee;
  16. display: grid;
  17. grid-template-columns: 80px 200px;
  18. gap: 10px;
  19. place-content: center center;
  20. }
  21. button {
  22. grid-column: 2/3;
  23. height: 26px;
  24. }
  25. button:hover {
  26. color: white;
  27. background-color: #bbbbbb;
  28. border: none;
  29. cursor: pointer;
  30. }
  31. .red {
  32. color: red;
  33. }
  34. </style>

效果预览:

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