Blogger Information
Blog 36
fans 0
comment 0
visits 28198
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jQuery基础知识实例
phpcn_u202398
Original
874 people have browsed it

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="jquery-3.4.1.js"></script>
  8. </head>
  9. <body>
  10. <ul id="first">
  11. <li class="item">item1</li>
  12. <li class="item">item2</li>
  13. <li class="item">item3</li>
  14. <li class="item">item4</li>
  15. <li class="item">item5</li>
  16. </ul>
  17. <ul id="second">
  18. <li class="item">item1</li>
  19. <li class="item">item2</li>
  20. <li class="item">item3</li>
  21. <li class="item">item4</li>
  22. <li class="item">item5</li>
  23. </ul>
  24. </body>
  25. </html>
  26. <script>
  27. var lg = console.log.bind(console);
  28. //1.$(上下文选择器);
  29. $("#first li").css("color","red");
  30. $("#first li:first-of-type").css("color","green");
  31. //2.$(js对象)
  32. var lis = document.querySelectorAll("#second li");
  33. lis.forEach(function(item){
  34. item.style.backgroundColor ="pink";
  35. });
  36. $(lis).css("background-color","lightgreen");
  37. //3.$(html文档)
  38. $("<h3>列表一<h3>").insertBefore("#first");
  39. $("<h3>列表二<h3>").insertAfter("#first");
  40. //4.$(callback)
  41. $(function(){
  42. $(document.body).css({
  43. "background-color":"lightblue",
  44. "font-size":"20px",
  45. });
  46. });
  47. </script>

2、jQuery查询结果的处理方式

  • toArray:将查询结果转为数组
  • $.each()
  • $.map():必须要有返回值
  • index():返回jQuery查询集合中的索引

    代码示例
    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. </head>
    9. <body>
    10. <ul id="first">
    11. <li class="item">item1</li>
    12. <li class="item">item2</li>
    13. <li class="item">item3</li>
    14. <li class="item">item4</li>
    15. <li class="item">item5</li>
    16. </ul>
    17. </body>
    18. </html>
    19. <script>
    20. var lg = console.log.bind(console);
    21. //1.toArray():
    22. var lis = $("ul > li");
    23. //lg(lis);
    24. for(var i = 0;i<lis.length;i++){
    25. lis.get(i).style.color = "green";
    26. }
    27. lis.toArray().forEach(function(item,index){
    28. if(index >= 3 ) lg("元素"+ index + ":" + item);
    29. });
    30. //2.$.each()
    31. lis.each(function(index ,value){
    32. //原生
    33. this.style.color = "green";
    34. //jQuery
    35. $(this).css("color","blue");
    36. });
    37. //$.map()
    38. var arr = $.map(lis,function(value,index){
    39. if(index % 2) return value;
    40. });
    41. //lg(arr);
    42. $(arr).css("background-color","red");
    43. //4.index()
    44. lis.click(function(){
    45. lg("点击第几个",$(this).index());
    46. });
    47. </script>

学习总结

本节课我们学习了$()工厂函数的四种使用场景和jQuery查询结果的处理方式,以前只是通过视频学习,知道一些简单的用法,对知识内容不是太懂。通过本节课的学习完善了知识点,对使用方法和场景更加清晰。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:jquery知识点并不多, 做好总结 , 很快就可以掌握常用的技能
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