经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jq初始,选择器,事件,内容操作,样式操作
来源:cnblogs  作者:小小咸鱼YwY  时间:2019/8/7 8:47:51  对本文有异议

jq操作页面文档http://jquery.cuishifeng.cn/

jq初始

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jq初始</title>
  6. </head>
  7. <body>
  8. <h1>jQuery就是js的工具库 - 一系列功能的集合体</h1>
  9. <h2>jq内部语法采用的就是原生js</h2>
  10. <h2>jq环境如何搭建 - 在需要使用jq的html中引入jquery.js即可</h2>
  11. <h2>jq就是优化了原生js鱼页面进行交互的逻辑</h2>
  12. </body>
  13. <!-- CDN 连接 外部资源 -->
  14. <!--<script src="https://code.jquery.com/jquery-3.4.1.js"></script>-->
  15. <!--<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>-->
  16. <script src="js/jquery-3.4.1.js"></script>
  17. <script>
  18. // jQuery对象
  19. console.log(jQuery);
  20. console.log($);
  21. console.log(Owen);
  22. console.log($('h1'));
  23. $('h1').click(function () {
  24. $('h1').css('color', 'red')
  25. })
  26. </script>
  27. </html>

jq选择器

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <div id="d" class="box"></div>
  9. <input type="text" id="d2" class="box" />
  10. <h3 class="h3"></h3>
  11. </body>
  12. <script src="js/jquery-3.4.1.min.js"></script>
  13. <script>
  14. // jq选择器:$('css选择器语法')
  15. let $div = $('#d');
  16. console.log($div);
  17. let $boxs = $('.box');
  18. console.log($boxs);
  19. // jq对象如何转换为js对象 - jq对象可以理解为装有js对象的数组
  20. // 就是通过索引取值
  21. let div = $div[0];
  22. console.log(div);
  23. console.log(document.getElementsByClassName('box')[0]);
  24. console.log(document.querySelectorAll('.box')[0]);
  25. console.log($div[0]);
  26. console.log($boxs[0]);
  27. console.log($boxs[1]);
  28. // js如何转换为jq对象
  29. let $newDiv = $(div);
  30. console.log($newDiv);
  31. </script>
  32. </html>

jq事件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jq事件</title>
  6. <style>
  7. .box {
  8. width: 200px;
  9. height: 200px;
  10. background-color: orange;
  11. margin-bottom: 10px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="box">1</div>
  17. <div class="box">2</div>
  18. </body>
  19. <script src="js/jquery-3.4.1.min.js"></script>
  20. <script>
  21. let $box = $('.box');
  22. // $box.click(function () {
  23. // // this就是被点击的标签 - js对象
  24. // console.log(this);
  25. // console.log($(this));
  26. // });
  27. // jq对象可以完成事件的批量绑定
  28. $box.on('click', function () {
  29. console.log(this);
  30. console.log(this.innerText);
  31. console.log($(this));
  32. });
  33. // js必须手动循环 绑定
  34. // let boxs = document.querySelectorAll('.box');
  35. // for (box of boxs) {
  36. // box.onclick = function () {
  37. // console.log(this);
  38. // console.log($(this));
  39. // }
  40. // }
  41. </script>
  42. </html>

jq内容操作

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jq内容操作</title>
  6. </head>
  7. <body>
  8. <h1 class="title">标题</h1>
  9. <input type="text" class="title">
  10. <button class="btn">改标题</button>
  11. </body>
  12. <script src="js/jquery-3.4.1.min.js"></script>
  13. <script>
  14. // js - jsObj.value | jsObj.innerText | jsObj.innerHTML
  15. // jq - jqObj.val() | jqObj.text() | jqObj.html()
  16. // 取值:jqObj.text() | jqObj.text('新值') | jqObj.text(fn)
  17. let $btn = $('.btn');
  18. let $inp = $('input.title');
  19. let $h1 = $('h1.title');
  20. $btn.click(function () {
  21. let val = $inp.val();
  22. if (val) {
  23. // $h1.text(val);
  24. $h1.html(val);
  25. $inp.val(function (index, oldValue) {
  26. // return oldValue.toUpperCase()
  27. return ''
  28. })
  29. }
  30. })
  31. </script>
  32. </html>

jq样式操作

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jq样式操作</title>
  6. <style>
  7. .box {
  8. /*width: 200px;*/
  9. height: 200px;
  10. background-color: pink;
  11. }
  12. .sup-box {
  13. /*width: 400px;*/
  14. height: 100px;
  15. background-color: orange;
  16. border-radius: 50%;
  17. line-height: 100px;
  18. text-align: center;
  19. color: red;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="box" style="width: 600px">文本</div>
  25. </body>
  26. <script src="js/jquery-3.4.1.min.js"></script>
  27. <script>
  28. let $box = $('.box');
  29. $box.click(function () {
  30. // 获取
  31. let width = $box.css('width');
  32. console.log(width);
  33. // 单个设置
  34. $box.css('background-color', function (i, o) {
  35. console.log(o);
  36. return 'red'
  37. });
  38. // 多条设置
  39. $box.css({
  40. width: '100px',
  41. height: '100px',
  42. backgroundColor: 'blue',
  43. });
  44. if ($box.hasClass('sup-box')) {
  45. $box.removeClass('sup-box')
  46. } else {
  47. $box.addClass(function (i, o) {
  48. console.log(i, o);
  49. return 'sup-box'
  50. })
  51. }
  52. })
  53. </script>
  54. <script>
  55. // localStorage['name'] = 'owen';
  56. // sessionStorage['age'] = 18;
  57. </script>
  58. </html>

原文链接:http://www.cnblogs.com/pythonywy/p/11305663.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号