• jQuery的使用


    目录

    jquery对象:

    jquery作为一般函数调用参数:

    jquery事件机制 

    jquery dom操作


    jquery对象:

    1. 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>Documenttitle>
    7. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js">script>
    8. <script>
    9. // 点击按钮将输入在输入框中的值弹框显示
    10. window.onload = function(){
    11. var input = document.querySelector('input');
    12. var btn = document.querySelector('button');
    13. btn.onclick = function(){
    14. alert(input.value)
    15. }
    16. }
    17. $(function(){
    18. //成为jquery实例:$()
    19. $('button:last').click(function(){
    20. alert($('input').val())
    21. })
    22. })
    23. script>
    24. head>
    25. <body>
    26. <input type="text">
    27. <button>确定(js)button>
    28. <button>确定(jquery)button>
    29. body>
    30. html>

     浏览器运行结果如下:


    jquery作为一般函数调用参数:

      1.$() 匿名函数  入口函数区别
      2.css选择器字符串 匹配元素 
      3.dom对象 jquery会把dom对象封装为jquery对象
      4.html字符串 表示创建html中元素

    1. 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>Documenttitle>
    7. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js">script>
    8. <script>
    9. /**
    10. * jquery函数被当作一般函数调用 $(params)
    11. * 1.函数内部参数可以是css选择器字符串 表示匹配元素
    12. * $('div') $('.two') $('#three')
    13. * 2.函数内部参数可以是dom对象 将他封装成jquery对象
    14. * 3.函数内部参数可以是匿名函数 表示jquery入口函数
    15. * 4.函数内部可以是html元素字符串 表示创建html元素
    16. */
    17. $(function(){
    18. $('button').click(function(){
    19. console.log(this,$(this));
    20. $(this).html('不想被网爆');
    21. //创建h1标题并追加给body标签
    22. $('

      sb

      '
      ).appendTo('body')
    23. })
    24. })
    25. script>
    26. head>
    27. <body>
    28. <button>网爆我button>
    29. body>
    30. html>

    浏览器运行结果如下:

     


    jquery事件机制 

      绑定事件:on(function(event){
        event---jquery封装的事件对象 data
      })  bind()  one()一次性事件绑定
      解绑事件:off()  unbind()  
      事件类型:click() blur() focus() mouseenter() mousedown() 
      trigger 模拟事件 

    1. 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>Documenttitle>
    7. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js">script>
    8. <script>
    9. /**
    10. * 1.给元素绑定事件 click(handler)
    11. * 2.给元素绑定事件 on(事件类型,实际参数,handler(形式参数){})
    12. */
    13. $(function () {
    14. // //绑定事件
    15. // $('button').on('click', [1, 2, 3, 4], function (event) {
    16. // console.log(event, '事件对象', event.data);
    17. // });
    18. // //解绑事件 off解绑事件
    19. // $('button').off('click');
    20. // //绑定事件 bind方法绑定事件
    21. // $('button').bind('click', 'terry', function (event) {
    22. // console.log(event);
    23. // });
    24. // $('button').bind('mouseenter', function (event) {
    25. // $(this).css({
    26. // backgroundColor:'red'
    27. // })
    28. // });
    29. // //unbind解绑事件
    30. // $('button').unbind('click');
    31. // //一次性解绑所有事件 unbind不加参数
    32. // $('button').unbind();
    33. // // $('button').on('click',function(){
    34. // // $(this).html('被点击了')
    35. // // });
    36. // //事件代理 给父元素绑定事件 on(事件类型,选择器,实际参数,handler)
    37. // $('body').on('click','button',function(){
    38. // $(event.target).html('被点击了')
    39. // })
    40. // //一次性事件绑定 one 事件只执行一次
    41. // $('button').one('click',{name:'larry'},function(event){
    42. // console.log(event.data);
    43. // })
    44. // //模拟事件 trigger
    45. // $('button').click(function(event,a,b,c){
    46. // console.log('我被点击了',event,a,b,c);
    47. // });
    48. // $('button').trigger('click',[1,2,3]);
    49. // //聚焦事件 focis
    50. // $('input').focus(function () {
    51. // $(this).css({
    52. // backgroundColor: 'red'
    53. // })
    54. // })
    55. // //失焦事件 blur
    56. // $('input').blur(function () {
    57. // $(this).css({
    58. // backgroundColor: 'blue'
    59. // })
    60. // })
    61. $('button').mouseenter(function(){
    62. $(this).css({
    63. backgroundColor:'red'
    64. })
    65. });
    66. $('button').mouseleave(function(){
    67. $(this).css({
    68. backgroundColor:'blue'
    69. })
    70. });
    71. $('button').dblclick(function(){
    72. console.log('我被双击了');
    73. });
    74. })
    75. script>
    76. head>
    77. <body>
    78. <input type="text">
    79. <button>点击我button>
    80. <button>点击我button>
    81. <button>点击我button>
    82. <button>点击我button>
    83. <button>点击我button>
    84. <button>点击我button>
    85. <button>点击我button>
    86. <button>点击我button>
    87. <button>点击我button>
    88. <button>点击我button>
    89. <button>点击我button>
    90. <button>点击我button>
    91. <button>点击我button>
    92. body>
    93. html>

    浏览器运行结果如下:


    jquery dom操作

      addClass() 添加类名
      removeClass() 移除类名
      toggleClass() 切换类名 有类名则是移除 没有则是添加
      clone() 深克隆和浅克隆 克隆事件和内容
      attr()
      removeAttr()
      html() 元素的内容 包括文本和标签
      text() 只获取元素的文本内容
      val()  获取输入框值

    1. 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>Documenttitle>
    7. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js">script>
    8. <style>
    9. .active{
    10. background-color: pink;
    11. }
    12. style>
    13. <script>
    14. $(function(){
    15. //在匹配到的元素后面增加内容 append
    16. $('div').append('我是新增的内容');
    17. $('div').append('

      一级标题

      '
      );
    18. //appendTo 将前面创建的元素添加给appendTo()中的目标元素
    19. $('

      段落标签

      '
      ).appendTo('body');
    20. $('div').after('
      块级元素
      '
      );
    21. $('div').before('
      块级元素
      '
      );
    22. $('button').click(function(){
    23. alert('我被点击了')
    24. });
    25. //clone 克隆节点 true深克隆(内容事件都可克隆) false浅克隆(事件没有克隆)
    26. $('button').clone(true).appendTo('body');
    27. // //添加一个类名 addClass
    28. // $('#one').addClass('active');
    29. // //移除一个类名 removeClass
    30. // $('#one').removeClass('two');
    31. //切换类名
    32. // $('div').click(function(){
    33. // //toggleClass 切换类名 如果默认有类名则是移除,如果没有则是添加
    34. // $(this).toggleClass('active');
    35. // })
    36. console.log($('body').text(),'元素文本内容');
    37. console.log($('body').html(),'识别代码片段');
    38. })
    39. script>
    40. head>
    41. <body>
    42. <button>点击我button>
    43. <div id="one" class="two" title="我是div的title">我是块级元素div>
    44. body>
    45. html>

    浏览器运行结果如下:

     


  • 相关阅读:
    软件测试架构师的工作日常
    Raft分布式一致性协议基本过程
    5.2 空表实验
    严选算法模型质量保障
    【UCIe】UCIe Sideband 介绍
    蓝桥杯嵌入式第二篇配置按键
    CSDN云IDE初次测评体验
    【学习笔记之数据结构】顺序表
    k8s之client-go和ctrl的各种k8s client
    Java 集合之 Set 接口
  • 原文地址:https://blog.csdn.net/l12345666777/article/details/134733671