博客列表 >03月04日作业:jQuery对html、css、自定义属性和常用DOM操作

03月04日作业:jQuery对html、css、自定义属性和常用DOM操作

李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰原创
2020年03月08日 20:31:59885浏览

作业一

一、常用属性操作知识点:
1、属性:

  • getter():获取,setter():设置操作

2、html属性:

  • attr():获取或者设置html元素属性
  • removeAtter():移除html元素属性

3、css属性:

  • css():获取元素的最终样式
  • addclass():添加类样式,与classList.add()功能类似
  • removeclass():删除类样式,与classList.remove()功能类似
  • toggleclass():自动切换类样式,与classList.toggle()功能类似
  • hasclass():判断是否有指定样式,classList.contains()功能类似

4、表单操作
val():获取或者设置表单元素的值
5、元素的内容

  • text():文本内容,innerText功能类似
  • html():可解析带有html标签的文本内容,innerHTML

6、元素的自定义属性值:

  • data():获取和设置元素的数据
  • removeData():删除元素中的数据

二、jQuery对DOM操作
1、插入和替换
append()|appendTo():尾部插入元素
prepend()|prependTo():头部插入元素
after()|inserAfter():后面插入元素
befor()|insertBefor():前面插入元素
replaceWith()|replaceAll():替换目标元素内容
2、复制/克隆元素
clone():创建并返回每一个选中元素的副本
3、删除元素
empty():将当前元素的所有子元素清空/删除
remove():将当前元素以及素有的子元素全部删除
4、其他知识点:
toLowerCase();字符串全部转小写;

作业二

1.1、代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <script src="//upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.3.min.js"></script>
  6. <title>Document</title>
  7. <style>
  8. body {
  9. display: flex;
  10. flex-direction: column;
  11. align-items: center;
  12. color: #666;
  13. }
  14. form {
  15. width: 400px;
  16. padding: 20px 10px;
  17. border: 1px solid #aaa;
  18. box-sizing: border-box;
  19. box-shadow: 0 0 5px #888;
  20. background-color: #eee;
  21. display: grid;
  22. grid-template-columns: 80px 200px;
  23. gap: 10px;
  24. place-content: center center;
  25. }
  26. button {
  27. grid-column: 2 / 3;
  28. height: 26px;
  29. }
  30. button:hover {
  31. color: white;
  32. background-color: #000;
  33. border:none;
  34. cursor: pointer;
  35. }
  36. .red {
  37. color: red;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <h2 class="red">用户登录</h2>
  43. <form action="handle.php" method="post">
  44. <label for="email">Email:</label>
  45. <input type="email" name="email" id="email" autofocus value="lectur@php.cn">
  46. <label for="password">Password</label>
  47. <input type="password" name="password" id="password" placeholder="不少于6位">
  48. <label for="confirm">记住我:</label>
  49. <div>
  50. <input type="radio" name="save" id="confirm" value="1" checked><label for="confirm">保存</label>
  51. <input type="radio" name="save" id="cancel" value="0"><label for="cancel">取消</label>
  52. </div>
  53. <button>登录</button>
  54. </body>
  55. <script>
  56. var form=$('form');
  57. // 1、attr() 一个参数事获取属性值,两个参数(属性名,新的属性值)
  58. console.log(form.attr('action'));
  59. // console.log(form.attr('action','admin.php'))
  60. // 同时操作多个,可以用对象字面量作为参数
  61. // form.attr({
  62. // 'action':'admin.php',
  63. // 'method':'get'
  64. // })
  65. form.attr('method','GET');
  66. form.attr('method',function(){
  67. var method=$(this).attr('method').toLowerCase();
  68. if(method==='get'){
  69. $(this).attr('action','query.php?id=1');
  70. }
  71. return 'POST';
  72. })
  73. form.removeAttr('action');
  74. // 2、关于CSS的操作
  75. console.log(form.css('width'));
  76. console.log(form.css('border'));
  77. console.log(form.css('border-style'));
  78. console.log(form.css('border-color'));
  79. $('h2').removeClass('red');
  80. $('h2').addClass('red');
  81. $('button').toggleClass('red');//如果有则删除,没有则添加
  82. // $('button').toggleClass('red');
  83. if($('button').hasClass('red')){
  84. console.log('有red类样式');
  85. }
  86. $('#email').val('ldy@php.com');//并没有改变html中的值,只是在渲染html页面时替换了
  87. $('#email').val(function(){
  88. return this.defaultValue;
  89. });
  90. $('h2').text('登陆表单');//添加文本内容,无法识别html标签元素
  91. $('h2').html('登录<span style="color:green">表单</span>');//可以识别html元素;
  92. var form=document.forms.item(0);
  93. console.log(form.getBoundingClientRect());//包含x,y坐标,上下左右(距父级起点的距离)和宽高
  94. console.log($('form').offset());//只有上,左,离顶部的距离
  95. $('body').css('height','2000px');
  96. // $(document).on('scroll',function(){
  97. // console.log($(document).scrollTop());
  98. // });
  99. form.dataset.src='1';
  100. console.log($(form).data('src'));
  101. // $(form).data('src','2');
  102. $(form).removeData('src');
  103. // $(form).data('src', 'PHP中文网欢迎你');
  104. </script>
  105. </html>

1.2、运行结果图:

2.1代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <script src="//upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.3.min.js"></script>
  6. <title>Document</title>
  7. <style>
  8. .action{
  9. color:red;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <script>
  15. $('body').append('<ol>');
  16. // $('body').prepend('<ol>');
  17. // $('<li>').text('华为手机').appendTo('ol:last-child()');
  18. // $('<li>').text('小米手机').prepednTo();
  19. $('<li>').text('小米手机').appendTo('ol:first-child()');
  20. $('<li>').addClass('action').html('<span>智能手机</span').appendTo('ol');
  21. // 添加元素的同时可以添加样式属性
  22. $('<li>', {
  23. 'id': 'test',
  24. 'style': 'background-color: yellow'
  25. }).html('<a href="" alt="">最新男装</a>').appendTo('ol');
  26. $('ol').append(function(){
  27. var lis='';
  28. for(var i=0;i<5;i++){
  29. lis+='<li>最新产品'+(i+1)+'</li>';
  30. }
  31. return lis;
  32. });
  33. $('ol > li:nth-child(3)').before('<li>新元素1</li>');
  34. $('ol > li:nth-child(3)').after('<li>新元素2</li>');
  35. $('<li>新元素3</li>').insertBefore('ol > li:first-child()');
  36. $('ol>:last-of-type()').replaceWith('<h4>替换元素</h4>');
  37. $('<h4>我才是第一个</h4>').replaceAll('ol > li:first-of-type');
  38. var ul=$('<ul>').appendTo('body');
  39. ul.append(function(){
  40. return $('ol:first-of-type > li:nth-of-type(-n+4)').clone();
  41. });
  42. console.log($('ol>li:nth-of-type(-n+2)'));
  43. // $(ul).empty();
  44. $(ul).remove();
  45. </script>
  46. </body>
  47. </html>

2.1运行效果图:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议