03.基本选择器.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>基本选择器</title>
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  9. <script>
  10. // 如果JQ代码写在body前面,必须将代码写在ready事件中,ready事件表示所有DOM加载完成之后才执行
  11. // $(document).ready(function(){
  12. // console.log($('.box'))
  13. // })
  14. // 简化写法
  15. // $(function(){
  16. // console.log($('.box'))
  17. // })
  18. // $(function(){//ready事件可以写多个,相当于JS中的onload事件,但是跟onload的区别是:可以出现多次,而onload在一个页面中只能出现一次。
  19. // console.log($('.box'))
  20. // })
  21. // id
  22. // $(function(){
  23. // $('#box').css({
  24. // color:'blue',
  25. // background:'#eee'
  26. // })
  27. // })
  28. // class
  29. // $(function(){
  30. // $('.box').css({
  31. // color:'red',
  32. // background:'#eee',
  33. // 'margin-bottom':'10px'
  34. // })
  35. // })
  36. // 标签
  37. // $(function(){
  38. // $('p').css({
  39. // color:'red',
  40. // background:'#eee',
  41. // 'margin-bottom':'10px'
  42. // })
  43. // })
  44. // 星号(*)
  45. // $(function(){
  46. // console.log($('*'))
  47. // })
  48. // 逗号(,)
  49. $(function(){
  50. $('#box,.box,p').click(function(){
  51. console.log($(this).html())
  52. })
  53. })
  54. </script>
  55. </head>
  56. <body>
  57. <div class="box">class1</div>
  58. <div class="box">class2</div>
  59. <div class="box">class3</div>
  60. <div id="box">id</div>
  61. <p>p标签1</p>
  62. <p>p标签2</p>
  63. <p>p标签3</p>
  64. </body>
  65. <!-- <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  66. <script>
  67. console.log($('.box'))
  68. </script> -->
  69. </html>