• jQuery笔记一


    目录

    一 jQuery的基本使用

    二 了解jQuery函数与jQuery对象

    (1)jQuery核心函数

    (2)jQuery核心对象

     三 选择器

    (1)基本选择器

    (2)层次选择器

    (3)过滤选择器

    (4)菜单选择器

    四 $工具方法

    五 属性


    一 jQuery的基本使用

    1. 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 type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js">script>
    9. <script>
    10. //绑定文档加载完成的监听
    11. // 2.使用jQuery核心函数($/jQuery)
    12. $(function(){
    13. /* 3.使用jQuery核心对象:执行$()返回的对象
    14. var $btn2 = $('#btn2')
    15. $btn2. click(function(){
    16. })
    17. */
    18. $('#btn2').click(function(){//给btn2绑定点击监听
    19. var username=$('#username').val()
    20. alert(username)
    21. })
    22. })
    23. script>
    24. head>
    25. <body>
    26. 用户名:<input type="text" id="username">
    27. <button id="btn2">确定(jQuery版)button>
    28. body>
    29. html>

    二 了解jQuery函数与jQuery对象

    1. <script type= "text/javascript" >
    2. console. log(jQuery===$) // true
    3. //2. jQuery对象:执行jQuery函数得到它
    4. console.1og($() instanceof Object) // true
    5. /*
    6. (function (window) {
    7. var jQuery = function () {
    8. return new xxx()
    9. }
    10. window.$ = window.jQuery = jQuery
    11. })(window)
    12. */
    13. script>

    (1)jQuery核心函数

    简称: jQuery 函数($/jQuery)
    jQuery库向外直接暴露的就是$/jQuery
    引入jQuery库后,直接使用$即可

    作为一般函数调用: $(param)
    1).参数为函数:当DOM加载完成后, 执行此回调函数
    2)参数为选择器字符串:查找所有匹配的标签, 并将它们封装成jQuery对象
    3).参数为DOM对象:将dom对象 封装成jQuery对象
    4).参数为html 标签字符串(用得少):创建标签对象并封装成jQuery对象


    作为对象使用: $.xxx()
    1). $.each() :隐式遍历数组
    2). $.trim() :去除两端的空格
     

    1. 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 type="text/javascript" src="js/jquery.min.js">script>
    9. <script type="text/javascript">
    10. // 需求1.点击按钮:显示按钮的文本,显示一 -个新的输入框
    11. //1.1).参数为函数:当DOM加载完成后, 执行此回调函数
    12. $(function () { //绑定文档加载完成的监听
    13. // 1.2). 参数为选择器字符串:查找所有匹配的标签,并将它们封装成jQuery对象
    14. $('#btn').click(function () { //绑定点击事件监听
    15. // this是什么?发生事件的dom元素(
    16. // alert(this. innerHTML)
    17. // 1.3).参数为DOM对象: 将dom对象 封装成jQuery对象
    18. alert($(this).html())
    19. // 1.4). 参数为html标签字符串(用得少):创建标签对象并封装成jQuery对象
    20. $('
      '
      ).appendTo('div')
    21. })
    22. })
    23. /*需求2. 遍历输出数组中所有元素值*/
    24. var arr=[1,3,5,7,9]
    25. // 1).$.each() :隐式遍历数组
    26. $.each(arr, function (index, item) {
    27. console.log(index,item)
    28. })
    29. // 2). $.trim() :去除两端的空格
    30. var str ='my school'
    31. // console.log('--- '+str. trim()+'---')
    32. console.log('---'+$.trim(str)+'---')
    33. script>
    34. head>
    35. <body>
    36. <div>
    37. <button id="btn">点一下button>
    38. <br/>
    39. <input type="text" msg="msg1"/><br/>
    40. <input type="text" msg="msg2"/><br/>
    41. body>
    42. html>

    (2)jQuery核心对象

    简称: jQuery对象
    得到jQuery对象:执行jQuery函数返回的就是jQuery对象j

    jQuery对象内部包含的是dom元素对象的伪数组(可能只有一个元素)
    使用jQuery对象: $obj.xxx()  

    基本行为:
    size()/length:包含的DOM元素个数
    [index]/get(index):得到对应位置的DOM元素
    each():遍历包含的所有DOM元素
    index():得到在所在兄弟元素中的下标
     

    1. 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. head>
    9. <body>
    10. <button>测试一button>
    11. <button>测试二button>
    12. <button id="btn3">测试三button>
    13. <button>测试四button>
    14. <script type="text/javascript" src="js/jquery.min.js">script>
    15. <script type="text/javascript">
    16. //需求1.统计一共有多少个按钮
    17. var $buttons = $('button')
    18. /*size()/length:包含的DOM元素个数*/
    19. console. log($buttons.size(), $buttons.length)//4 4
    20. //需求2.取出第2 个button的文本
    21. /*[index]/get(index):得到对应位置的DOM元素*/
    22. console.log($buttons [1] .innerHTML, $buttons.get(1).innerHTML)
    23. //需求3.输出所有button 标签的文本
    24. /*each():遍历包含的所有DOM元素*/
    25. /*$buttons.each(function (index, domELe) {
    26. console.log(index, domELe.innerHTML,this)
    27. //此处this是
    28. })*/
    29. $buttons.each(function () {
    30. console.log(this.innerHTML)
    31. })
    32. //需求4.输出'测试三'按钮是所有按钮中的第几个
    33. console.log($('#btn3').index()) //2
    34. script>
    35. body>
    36. html>


    伪数组:object对象,有length属性和数值下标属性;没有数组特别的方法如forEach(), push(), pop(), splice()等

     三 选择器

    选择器本身只是一个有特定语法规则的字符串,没有实质用处

    只有调用$(),并将选择器作为参数传入才能起作用

    $ (selector)作用:根据选择器规则在整个文档中查找所有匹配的标签的伪数组,并封装成jQuery对象返回

    (1)基本选择器

    (2)层次选择器

    层次选择器:查找子元素,后代元素,兄弟元素的选择器
    1. ancestor descendant
    在给定的祖先元素 下匹配所有的后代元素
    2. parent>child
    在给定的父元素 下匹配所有的子元素
    3. prev+next
    匹配所有紧接在 prev元素后的next元素
    4. prev~siblings
    匹配prev元素之后的所有siblings 元素

    (3)过滤选择器

    定义:在原有选择器匹配的元素中进--步进行过滤的选择器

    1. 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. head>
    9. <body>
    10. <div id="div1" class="box" >class为box的div1div>
    11. <div id="div2" class="box" >class为box的div2div>
    12. <div id="div3" >div3div>
    13. <span class="box">class为box的span
    14. <br/>
    15. <ul>
    16. <li>AAAAAli>
    17. <li title="hello">BBBBBli>
    18. <li class= "box">CCCCCli>
    19. <li title= "hello" >DDDDDli>
    20. <li title= "two">BBBBBli>
    21. <li style="display:none">我本来是隐藏的li>
    22. ul>
    23. <script type="text/javascript" src="../js/jquery.min.js">script>
    24. <script type="text/javascript">
    25. //1.选择第-个div
    26. // $('div:first ').css( 'background', 'red')
    27. //2.选择最后一个class为box的元素
    28. //$('.box:last'). css( 'background', 'red')
    29. //3.选择所有class属性不为box的div
    30. // $('div:not(. box) '). css( 'background', 'red')
    31. //4. 选择第二个和第三个li元素(//多个过被选择器不是同时执行, 而是依次)
    32. //$('li:gt(0):lt(2)'). css('background', 'red')
    33. //$('li:lt(3):gt(0)'). css('background', 'red')
    34. //5.选择内容为BBBBB的Li
    35. // $('li:contains("BBB") ').css( 'background', 'red')
    36. //6.选择隐藏的li
    37. // console.log($('li:hidden').length, $('li:hidden')[e])
    38. //7.选择有title属性的li元素
    39. //$('li[title]' ).css('background', 'red')
    40. //8.选择所有属性title为hello的Li元素
    41. $('li[title= "hello"]').css('background', ' red')
    42. script>
    43. body>
    44. html>

    (4)菜单选择器

    1. 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. head>
    9. <body>
    10. <form>
    11. 用户名: <input type="text"/><br>
    12. 密码:<input type="password"/><br>
    13. 爱好:
    14. <input type=" checkbox" checked=" checked"/>篮球
    15. <input type=" checkbox"/>足球
    16. <input type=" checkbox" checked=" checked"/>羽毛球<br>
    17. 性别:
    18. <input type=" radio" name= "sex" value= ' male'/>
    19. <input type= "radio" name="sex" value= 'female' /><br>
    20. 邮箱:<input type="text" name="email" disabled="disabled"/><br>
    21. 所在地:
    22. <select>
    23. <option value="1">北京option>
    24. <option value="2" selected="selected" >天津option>
    25. <option value-="3" >河北option>
    26. se1ect><br>
    27. <input type="submit" value="提交"/>
    28. form>
    29. <script type="text/javascript" src="../js/jquery.min.js">script>
    30. <script type="text/javascript">
    31. //1.选择不可用的文本输入框
    32. // $(':text:disabled').css( 'background', 'red')
    33. //2.显示选择爱好的个数
    34. console.log($(':checkbox:checked').length)
    35. //3.显示选择的城市名称
    36. $(':submit').click(function () {
    37. var city = $('select>option:selected' ).html() //此时city值为天津
    38. city = $('select').val() //选择的option的value属性值
    39. alert(city)
    40. })
    41. script>
    42. body>
    43. html>

    四 $工具方法

    $.each():遍历数组或对象中的数据
    $.trim():去除字符串两边的空格
    $. type(obj):得到数据的类型
    $. isArray(obj):判断是否是数组
    $. isFunction(obj):判断是否是函数
    $. parseJSON(json) :解析json字符串转换为js对象/数组

    1. var json = '{"name" :"Tom", "age":12}' // json对象: {}
    2. // json对象===>JS对象
    3. console.1og($.parseJSON(json) )
    4. json ='[{"name":"Tom", "age":12}, {"name":"JACK", "age":13}]' // json数组: []
    5. // json数组===>JS数组
    6. console.1og($.parseJSON(json))
    7. /*
    8. JSON. parse(jsonString) json 字符串--->js对象/数组
    9. JSON. stFingify(js0bj/jsArr) js对象/ 数组--->json字符串
    10. */

    五 属性

    操作任意属性
    attr()、removeAttr()、prop()
    操作class属性
    addClass()、removeClass()
    操作HTML代码/文本/值
    html()、val()

    1. 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. head>
    9. <body>
    10. <div id="div1" class="box" title= "one" >class为box的div1div>
    11. <div id="div2" class="box" title= "two" >class为box的div2div>
    12. <div id="div3">div3div>
    13. <span class="box">class为box的spanspan>
    14. <br/>
    15. <ul>
    16. <li>AAAAA
    17. <li title="hello" class="box2" >BBBBBli>
    18. <li class="box">CCCCCli>
    19. <li title="hello">DDDDDli>
    20. <li title="two"><span>BBBBBspan>
    21. ul>
    22. <input type="text" name="username" value="hi"/>
    23. <br>
    24. <input type="checkbox">
    25. <input type="checkbox">
    26. <br>
    27. <button>选中button>
    28. <button>不选中button>
    29. <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js">script>
    30. <script>
    31. //读取第-个div的title属性
    32. //console. log($( 'div:first').attr('title')) // one
    33. //给所有的div 设置name属性(value为Jack)
    34. // $('div'). attr( 'name', 'Jack')
    35. //移除所有div的title属性
    36. // $('div'). removeAttr('title')
    37. //得到最后-个Li的标签体文本
    38. //console.log($('li:Last ').htm())
    39. // 设置第-个li的标签体为"

      mmmmmmmm

    40. //$( 'li:first'). html('

      mmmmmmmmm

      ')
    41. //得到输入框中的value值
    42. //console.log($(':text').val())
    43. //得到输入框中的value值
    44. //console.log($(':text').val()) //读取
    45. //将输入框的值设置为atguigu
    46. //$(':text').val( 'atguigu') /设置 读写合一
    47. //点击'全选'按钮实现全选
    48. // attr(): 操作属性值为非布尔值的属性
    49. // prop(): 专门操作属性值为布尔值的属性
    50. var $checkboxs = $(':checkbox')
    51. $('button:first'). click(function () {
    52. $checkboxs . prop('checked', true)
    53. })
    54. //12..点击'全不选' 按钮实现全不选
    55. $('button:last'). click(function () {
    56. $checkboxs. pro( 'checked ',false)
    57. })
    58. script>
    59. body>
    60. html>

  • 相关阅读:
    计算机图形学-算法总结
    carsim 2020 安装说明及多个版本冲突问题解决
    计算机毕业设计——基于SpringBoot框架的网上购书系统的设计与实现
    【云原生之Docker实战】使用Docker部署excalidraw白板绘图工具
    uni-app 实现拍照后给照片加水印功能
    力扣每日一题 网络信号最好的坐标
    归并排序-成绩输出-c++
    中国医疗器械在“一带一路”国家贸易状况及贡献度分析
    高并发之线程池
    [附源码]Python计算机毕业设计Django面包连锁店管理系统
  • 原文地址:https://blog.csdn.net/qq_62401904/article/details/126086131