jQuery是一个快速,简洁的JavaScript库,座右铭:少写,多做。

1. $()的四种类型参数的应用场景实例

  • $(筛选器) : 获取dom对象,相当于document.querySelectorAll
  • $(dom对象) : 把dom对象转化为jQuery对象
  • $(html文本) : 创建dom元素,返回为jQuery对象
  • $(callback) :当dom树加载完成后的回调
  1. <body>
  2. <ul>
  3. <li>item1</li>
  4. <li>item2</li>
  5. <li>item3</li>
  6. <li>item4</li>
  7. <li>item5</li>
  8. </ul>
  9. <script>
  10. // 1. $(筛选器) : 获取dom对象,相当于document.querySelectorAll
  11. console.log($('ul'));
  12. // 2. $(dom对象) : 把dom对象转化为jQuery对象
  13. console.log($(document.body) instanceof jQuery);
  14. // 3. $(html文本) : 创建dom元素,返回为jQuery对象
  15. $("<h2>Hello World</h2>").insertBefore($('ul'));
  16. // 4. $(callback) :当dom树加载完成后的回调
  17. $(() => $('h3').css('color', 'red'));
  18. </script>
  19. <h3>bye,bye</h3>
  20. </body>

2. jQuery常用方法

  • attr(name, value) : 属性操作,单参数时是获取 同比:getAttribute, 双参数是设置 同比: setAttribute,设置多个属性时可使用对象传参
  • removeAttr() : 移除属性 同比: removeAttribute
  1. <body>
  2. <ul name='lists' id="ul1" style="border: 1px solid #000;">
  3. <li>item1</li>
  4. <li>item2</li>
  5. <li>item3</li>
  6. <li>item4</li>
  7. <li>item5</li>
  8. </ul>
  9. </body>
  10. <script>
  11. const ul = $('ul');
  12. const li = $('li');
  13. // 获得属性
  14. console.log(ul.attr('name'));
  15. // 设置属性
  16. ul.attr('name', 'test');
  17. console.log(ul.attr('name'));
  18. // 多属性设置
  19. ul.attr({ 'name': 'test2', 'id': '1', 'data-index': 'shop' });
  20. console.log(ul.attr('id'));
  21. // 删除属性
  22. ul.removeAttr('id');
  23. console.log(ul.attr('id'));
  24. </script>
  • css(name, value) : 样式操作,单参数时是获取 同比:style, 双参数是设置 同比: style.name = value,设置多个属性时可使用对象传参
  1. <body>
  2. <ul name='lists' id="ul1" style="border: 1px solid #000; color: lightgreen;">
  3. <li>item1</li>
  4. <li>item2</li>
  5. <li>item3</li>
  6. <li>item4</li>
  7. <li>item5</li>
  8. </ul>
  9. </body>
  10. <script>
  11. const ul = $('ul');
  12. // 获取样式
  13. console.log(ul.css('color'));
  14. // 设置样式
  15. $('li:nth-of-type(1)').css('backgroundColor', 'yellow');
  16. // 设置多个样式
  17. $('li:nth-of-type(2)').css({ 'font-size': '20', 'backgroundColor': 'red' });
  18. </script>
  • addClass(name) : 添加样式类 同比:classlist.add
  • removeClass(name) : 删除样式类 同比:classlist.remove
  • hasClass(name) : 判断是否有某个样式类 同比:classlist.contains
  • toggleClass(name) : 切换样式类 同比:classlist.toggle
  1. <style>
  2. .error {
  3. background-color: red;
  4. }
  5. .warning {
  6. background-color: orange;
  7. }
  8. </style>
  9. <body>
  10. <ul>
  11. <li>item1</li>
  12. <li class=" warning">item2</li>
  13. <li>item3</li>
  14. <li>item4</li>
  15. <li>item5</li>
  16. </ul>
  17. </body>
  18. <script>
  19. // 添加样式类
  20. $('li:nth-of-type(1)').addClass('error');
  21. // 删除样式类
  22. $('li:nth-of-type(2)').removeClass('warning');
  23. // 判断是否有类
  24. console.log($('li:nth-of-type(2)').hasClass('warning'));;
  25. // 切换样式类
  26. $('li:nth-of-type(2)').on('click', function () {
  27. $(this).toggleClass('warning');
  28. });
  29. </script>
  • val() : 获取或者设置表单元素的value属性的值,无参获取, 有参数是设置
  • html() : html文本处理 无参获取, 有参数是设置 同比: innerHTML
  • text() : 文本处理 无参获取, 有参数是设置 同比:textContent
  1. <body>
  2. <form action="">
  3. <label for="" id="userlab">用户名:</label>
  4. <input type="text" id="username">
  5. <label for="" id="pwdlab">密码:</label>
  6. <input type="password" id="password">
  7. <div id='btn'></div>
  8. </form>
  9. </body>
  10. <script>
  11. // 设置value
  12. $('#username').val('php.com');
  13. // 获得value
  14. console.log($('#username').val());
  15. // 获得text
  16. console.log($('#userlab').text());
  17. // 设置text
  18. $('#pwdlab').text('password');
  19. // 设置html文本
  20. $('#btn').html('<Button type="button">提交在此</Button>');
  21. </script>

3. 将jQuery对象转js对象的方法

因为jQuery的限制性,无法完全替换js的一些操作或方法.需要将jQuery对象转为js对象.
jQuery对象转换成DOM对象:
jQuery对象.get(索引值);
jQuery对象[索引值]
jQuery对象是包装集(集合),从集合中取数据可以使用索引的方式
DOM对象转换成jQuery对象:
$(DOM对象) 只有这一种方法;

  1. <style>
  2. .cur {
  3. color: lightcoral;
  4. }
  5. .pre {
  6. color: lightgreen;
  7. }
  8. </style>
  9. <body>
  10. <ul>
  11. <li>item1</li>
  12. <li>item2</li>
  13. <li>item3</li>
  14. <li>item4</li>
  15. <li>item5</li>
  16. </ul>
  17. <script>
  18. // 同比于jQuery的addClass
  19. $('li')[3].classList.add('cur');
  20. $('li:nth-of-type(2)').addClass('pre');
  21. </script>
  22. </body>

更多相关文章

  1. jQuery方法使用
  2. 如何在TypeScript中的window上显式设置新属性?
  3. rxjs 的 observable 是什么?
  4. 【DB笔试面试660】在Oracle中,在编译存储过程、函数等对象时无响
  5. JS 中的 Reflect 和 Proxy
  6. fail2ban 防止暴力破
  7. 对象存储服务(Object Storage Service,OBS)
  8. 【linux】循序渐进学运维-CentOS7基本配置
  9. 不一样的享元模式(设计模式四)

随机推荐

  1. android Handlerr.removeCallbacksAndMes
  2. Android(安卓)Asynchronous Http Client-
  3. android tcpdump
  4. Android SQLite存取图像
  5. android 调用draw(canvas) 函数自动退出
  6. Android支持multiDexEnabled,自建脚本编译
  7. android 加载模式
  8. Android(安卓)布局 屏幕适配
  9. Android动态设置控件大小以及设定margin
  10. 【翻译】(26)Android如何绘画视图