Blogger Information
Blog 34
fans 0
comment 0
visits 21634
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月23日_ Jquery文档处理、筛选和一些其他的方法 - 九期线上班
只猫
Original
582 people have browsed it

Jquery文档处理

在内部添加 append(常用) appendTo prepend prependTo
在外部插入 after before insertAfter insertBefore
删除元素结点 empty(常用) remove
复制 clone(一般不用)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Jquery文档处理</title>
  6. <script src="jquery.js"></script>
  7. <style>
  8. .my{
  9. margin-bottom: 20px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div id="mydiv">
  15. <label for="myinput">input:</label>
  16. <input id="myinput" type="text"></input>
  17. </div>
  18. <div class="my" flag="mydiv"><span>城市:</span></div>
  19. <button onclick="appends()">append</button>
  20. <button onclick="appends_html()">append-html</button>
  21. <button onclick="appends_html2()">append-html2</button>
  22. <br>
  23. <button onclick="prepend()">prepend</button>
  24. <button onclick="appendTo()">appendTo</button>
  25. <button onclick="prependTo()">prependTo</button>
  26. <button onclick="after()">after</button>
  27. <button onclick="before()">before</button>
  28. <br>
  29. <button onclick="empty()">empty</button>
  30. <button onclick="remove()">remove</button>
  31. <button onclick="removeit(this)">移除</button>
  32. <script>
  33. //append() 内部 向后追加
  34. function appends() {
  35. //向div里面从后面追加
  36. $('div[flag="mydiv"]').append('郑州');
  37. }
  38. function appends_html()
  39. {
  40. //字符串html
  41. var html = '<select><option value="zz">郑州</option><option value="ly">洛阳</option>';
  42. //添加字符串和添加一段html文档的都是一样的用法
  43. $('div[flag="mydiv"]').append(html);
  44. }
  45. function appends_html2()
  46. {
  47. //使用html()获取mydiv中的html
  48. var myinput = $('#mydiv').html();
  49. //追加
  50. $('div[flag="mydiv"]').append(myinput);
  51. }
  52. //prepend() 元素内 向前追加
  53. function prepend() {
  54. //向div里面从前面追加
  55. $('div[flag="mydiv"]').prepend('郑州');
  56. }
  57. //appendTo() 相当于取走 放置
  58. function appendTo() {
  59. //插入的内容在函数前,容器在后
  60. $('.my>span').appendTo($('#mydiv'));
  61. }
  62. //prependTo() 在内部向前添加
  63. function prependTo() {
  64. //插入的内容在函数前,容器在后
  65. $('div[flag="mydiv"]').prependTo($('#mydiv'));
  66. }
  67. //在元素外部插入 兄弟结点
  68. //before after
  69. function after(){
  70. //字符串html
  71. var html = '<select><option value="zz">郑州</option><option value="ly">洛阳</option>';
  72. //添加字符串和添加一段html文档的都是一样的用法
  73. $('div[flag="mydiv"]').after(html);
  74. }
  75. function before(){
  76. //字符串html
  77. var html = '<select><option value="zz">郑州</option><option value="ly">洛阳</option>';
  78. //添加字符串和添加一段html文档的都是一样的用法
  79. $('div[flag="mydiv"]').before(html);
  80. }
  81. //empty remove 移除结点
  82. function empty(){
  83. //清空内部结点
  84. $('#mydiv').empty();
  85. }
  86. function remove(){
  87. //直接移除选中节点
  88. $('#mydiv').remove();
  89. }
  90. //使用this关键字
  91. function removeit(obj){
  92. console.log(obj);
  93. $(obj).remove();
  94. }
  95. </script>
  96. </body>
  97. </html>

筛选器 过滤 、添加移除属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jquery筛选</title>
  6. <script src="jquery.js"></script>
  7. <style>
  8. .active{color: #fff;background-color: red;}
  9. .hides{display: none;}
  10. </style>
  11. </head>
  12. <body>
  13. <div>
  14. <button id="b1">btn1</button>
  15. <button id="b2">btn2</button>
  16. <button id="b3">btn3</button>
  17. </div>
  18. <button attr="remove" onclick="remove1()">remove1</button>
  19. <button attr="remove" onclick="remove2()">remove2</button>
  20. <button attr="remove" onclick="remove3()">remove3</button>
  21. <div id="pro" style="background: #f1f1f1; height: 300px;">
  22. <a href="javascript:;" onclick="province(this);hasClass(this)" code="jiangsu">江苏</a>
  23. <a href="javascript:;" class="active" onclick="province(this);hasClass(this)" code="henan">河南</a>
  24. <a href="javascript:;" onclick="province(this);hasClass(this)" code="zhejiang">浙江</a>
  25. <a href="javascript:;" onclick="province(this);hasClass(this)" code="shanxi">陕西</a>
  26. </div>
  27. <div id="menu">
  28. <label onclick="toggleClass(this)">菜单</label>
  29. <div class="hides">
  30. <p>每日头条</p>
  31. <p>每日头条</p>
  32. <p>每日头条</p>
  33. </div>
  34. <p>每日视频</p>
  35. <p>每日视频</p>
  36. </div>
  37. <button onclick="children_()">children</button>
  38. <script type="text/javascript">
  39. //jquery筛选器
  40. //过滤 eq()、first()、last() hasClass()
  41. function remove1(){
  42. $('div button').first().remove();
  43. }
  44. function remove2(){
  45. $('div button').eq(1).remove();
  46. }
  47. function remove3(){
  48. $('div button').last().remove();
  49. }
  50. //hasClass()
  51. function hasClass(obj){
  52. var flag = $(obj).hasClass('active');
  53. console.log(flag);
  54. }
  55. //查找 children() 获取直接子元素
  56. function children_(){
  57. $('#menu').children('p').css('color','red');
  58. }
  59. //addClass添加class属性
  60. //removeClass移除class属性
  61. function province(obj){
  62. //每次点击先清空
  63. $('#pro a').removeClass('active');
  64. //点击添加active属性
  65. $(obj).addClass('active');
  66. //通过php $_GET[]拿到值
  67. //window.location.href="?province="+$(obj).attr('code');
  68. }
  69. //toggleClass 添加或者删除一个样式类 如果存在就删除 不存在就添加
  70. function toggleClass(obj){
  71. $('label + div').toggleClass('hides');
  72. }
  73. function myshow(obj){
  74. //绑定两个或多个处理程序绑定到匹配的元素,用来执行在交替的点击。
  75. //在jQuery1.9被移除 jQuery animation 也有一个名为.toggle()方法
  76. $('label + div').toggle();
  77. //添加style属性
  78. $('.hides').attr('style','display:block');
  79. //添加css
  80. $('.hides').css('display','block');
  81. //show()
  82. $('.hides').show(300);
  83. }
  84. </script>
  85. </body>
  86. </html>

案例:

省市联动

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>省市</title>
  6. <script src="jquery.js"></script>
  7. </head>
  8. <body>
  9. <div>
  10. <label for="province">籍贯:</label>
  11. <select name="province" id="province" onchange="chk_province()">
  12. <option value="">请选择</option>
  13. <option value="hn">河南</option>
  14. <option value="sd">山东</option>
  15. <option value="hb">湖北</option>
  16. <option value="zj">浙江</option>
  17. <option value="">...</option>
  18. </select>
  19. <label for="city">城市:</label>
  20. <select name="city" id="city">
  21. <option value="">请选择</option>
  22. </select>
  23. </div>
  24. <script type="text/javascript">
  25. //模拟数据
  26. function getcities(province){
  27. var cities = [];
  28. if(province==''){
  29. return cities;
  30. }
  31. if(province=='hn'){
  32. cities = [{val:'zz',txt:'郑州'},{val:'ly',txt:'洛阳'},{val:'kf',txt:'开封'}];
  33. }
  34. if(province=='zj'){
  35. cities = [{val:'hz',txt:'杭州'},{val:'jh',txt:'金华'},{val:'wz',txt:'温州'}];
  36. }
  37. return cities;
  38. }
  39. function chk_province(){
  40. var province = $('select[name="province"]').val();
  41. var cities = getcities(province);
  42. var html = '';
  43. //循环拿到数组的值
  44. for(var i=0;i<cities.length;i++){
  45. //添加到html
  46. html += ('<option value="'+cities[i].val+'">'+cities[i].txt+'</option>');
  47. console.log(cities[i]);
  48. }
  49. console.log(cities);
  50. console.log(html);
  51. //$('select[name="city"]').html(html);
  52. $('select[name="city"]').empty().append(html);
  53. }
  54. </script>
  55. </body>
  56. </html>

标签页

  1. <?php
  2. if(isset($_GET['aaa'])){
  3. $shengfen = $_GET['aaa'];
  4. }else{
  5. $shengfen = '';
  6. }
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <title>demo</title>
  13. <script src="jquery.js"></script>
  14. <style>
  15. .active{color: #fff;background-color: red;}
  16. </style>
  17. </head>
  18. <body>
  19. <div id="pro" style="background: #f1f1f1; height: 300px;">
  20. <a href="javascript:;" <?php echo $shengfen=='jiangsu'?'class="active"':''?>onclick="province(this)" code="jiangsu">江苏</a>
  21. <a href="javascript:;" <?php echo $shengfen=='henan'?'class="active"':''?>onclick="province(this)" code="henan">河南</a>
  22. <a href="javascript:;" <?php echo $shengfen=='zhejiang'?'class="active"':''?>onclick="province(this)" code="zhejiang">浙江</a>
  23. <a href="javascript:;" <?php echo $shengfen=='shanxi'?'class="active"':''?>onclick="province(this)" code="shanxi">陕西</a>
  24. </div>
  25. <script type="text/javascript">
  26. function province(obj){
  27. //每次点击先清空
  28. $('#pro a').removeClass('active');
  29. //点击添加active属性
  30. $(obj).addClass('active');
  31. //通过php $_GET[]拿到值
  32. window.location.href="?aaa="+$(obj).attr('code');
  33. }
  34. </script>
  35. </body>
  36. </html>

总结:jquery课程的第三天,讲到了很多jq类库的方法,都很灵活,通过练习逐渐上手。案例很适合练手,自己写还是有点难以写出来。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:jquery很好玩, 对不对?
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!