jQuery的基本使用

选择器

$('选择器')

  1. <p>我是p标签</p>
  2. <div class="div">
  3. <p>我是div中的p</p>
  4. <div>
  5. <p>我是div下的div的p</p>
  6. </div>
  7. </div>
  8. <script src="./jquery-3.5.1.min.js"></script>
  9. <script>
  10. // 选择器
  11. console.log($)
  12. $('p').css('backgroundColor', 'red')
  13. $('.div p').css('backgroundColor', 'skyblue')
  14. // DOM转为jQuery对象
  15. var p = document.querySelector('p');
  16. var $p = $(p)
  17. $p.css('backgroundColor', 'yellow')
  18. console.log($p)
  19. // 事件绑定
  20. $('p').click(function() {
  21. // this 表示触发事件的元素
  22. $(this).css('backgroundColor', 'green')
  23. })
  24. </script>

内容操作

html('') html()改变内部的html 读取html

text('') text() 改变文本内容 读取文本内容

  1. <div class="div1"></div>
  2. <div class="div2"></div>
  3. <script src="./jquery-3.5.1.min.js"></script>
  4. <script>
  5. // 返回的还是jQuery对象,意味着支持链式编程
  6. $('.div1').html('<a href="">你好</a>')
  7. $('.div2').text('<a href="">你好</a>')
  8. console.log($('.div1').html())
  9. console.log($('.div1').text())
  10. </script>

过滤方法

first()第一个元素

last()最后一个元素

eq(索引) 在遍历的时候获得指定索引的元素

  1. <!-- 过滤方法 first() last() eq(索引) 返回的都是jQuery对象-->
  2. <div class="box">
  3. <button>-</button>
  4. <span>0</span>
  5. <button>+</button>
  6. </div>
  7. <script src="./jquery-3.5.1.min.js"></script>
  8. <script>
  9. var span = $('.box>span')
  10. var num = span.text()
  11. // $('.box button:first-child')
  12. $('.box button').first().click(function() {
  13. console.log('减少')
  14. if (num > 0) {
  15. num--
  16. span.text(num)
  17. } else {
  18. alert('到底了');
  19. }
  20. })
  21. // $('.box button:last-child')
  22. $('.box button').last().click(function() {
  23. console.log('增加')
  24. if (span.text() < 10) {
  25. num++
  26. span.text(num)
  27. } else {
  28. alert('到头了');
  29. }
  30. })
  31. </script>

样式操作

css('属性名','属性值')给指定元素设置属性及属性值

css({}) 给指定元素设置样式 以对象的形式作为参数

css('属性名') 获取指定属性的值

  1. <style>
  2. p {
  3. color: #333;
  4. }
  5. div {
  6. width: 200px;
  7. height: 200px;
  8. margin-top: 20px;
  9. border: 1px solid #000;
  10. }
  11. </style>
  12. <h3>css方法</h3>
  13. <p>css方法可以设置和获取元素样式</p>
  14. <div class="box"></div>
  15. <script src="./jquery-3.5.1.min.js"></script>
  16. <script>
  17. // 设置样式
  18. $('.box').css('backgroundColor', 'pink')
  19. $('.box').css('border', '10px solid yellow')
  20. // 用对象的形式设置
  21. $('.box').css({
  22. 'width': 100,
  23. 'height': 100,
  24. 'backgroundColor': 'red'
  25. })
  26. // 获取样式属性值
  27. let width = $('.box').css('width')
  28. console.log(width)
  29. </script>

属性操作

  1. <a href="" self="123">百度</a>
  2. <script src="./jquery-3.5.1.min.js"></script>
  3. <script>
  4. // 设置属性的值
  5. $('a').attr('href', 'https:www.baidu.com')
  6. // 获取指定属性的值
  7. var hrefValue = $('a').attr('href')
  8. console.log(hrefValue)
  9. // 删除指定属性的值
  10. $('a').removeAttr('self')
  11. </script>

值的操作

  1. <input type="text" class="text">
  2. <script src="./jquery-3.5.1.min.js"></script>
  3. <script>
  4. $('.text').val('设置值');
  5. let value = $('.text').val();
  6. console.log('获得的值为:', value)
  7. </script>

元素的查找

  1. <!-- 元素查找
  2. 父元素 parent()
  3. 子元素 children() children('选择器')
  4. 兄弟元素 siblings() siblings('选择器')
  5. 后代元素 find('选择器')
  6. 返回的都是jQuery对象
  7. -->

类名操作

  1. <div class="box"></div>
  2. <script src="./jquery-3.5.1.min.js"></script>
  3. <script>
  4. // 默认是没有样式的,即使你添加了类名
  5. let has = $('.box').hasClass();
  6. console.log(has)
  7. $('.box').addClass('bg')
  8. // 移除所有的样式,如果不指定类名
  9. $('.box').removeClass('bg')
  10. $('.box').click(function() {
  11. $(this).toggleClass('bg')
  12. })
  13. </script>

事件

  1. <!--
  2. .事件名(function(){}) 注册事件
  3. on('事件名',function(){}) 注册事件
  4. off('事件名')移除指定的事件
  5. off()移除该元素的所有的事件
  6. one('事件名',function(){})注册事件一次性事件 只能怪触发一次
  7. -->
  8. <input type="text" class="text">
  9. <script>
  10. $('.text').on('input', function() {
  11. console.log($(this).val())
  12. })
  13. $('.text').off('input')
  14. $('.text').one('input', function() {
  15. alert($(this).val())
  16. })
  17. </script>
  1. <!--
  2. 直接触发事件 .事件名()
  3. .trigger('事件名') 可以触发所有的事件,包括自定义事件
  4. 定义自定义事件
  5. .on('自定义事件',function(){})
  6. -->
  7. <input type="text" class="text">
  8. <script>
  9. $('.text').click(function() {
  10. console.log('我被点击了')
  11. })
  12. // 触发事件
  13. $('.text').click()
  14. $('.text').on('my', function() {
  15. console.log('自定义事件触发了')
  16. })
  17. $('.text').trigger('my');
  1. <script>
  2. // 滚动事件 用jq的写法是 $(window).scroll(function(){})
  3. window.onscroll = function() {}
  4. // 点击事件 jq写法 $(window).click(){}
  5. window.onclick = function() {}
  6. </script>
版权声明:本文为沈先生爱猫咪原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/SYF--BLOG/p/16692201.html