Blogger Information
Blog 37
fans 1
comment 0
visits 27044
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1223_jquery文档处理与筛选 第38课
叮叮当当
Original
785 people have browsed it

1、插入

  1. <div id="hobby">
  2. <select name="hobby">
  3. <option value="eat">吃饭</option>
  4. <option value="sleep">睡觉</option>
  5. <option value="hit doudou">打豆豆</option>
  6. <option value="happy to hit doudou">快乐地打豆豆</option>
  7. </select>
  8. </div>
  9. <hr>
  10. <div id="part">
  11. <select>
  12. <option value="come around">串门</option>
  13. <option value="brag">吹牛</option>
  14. </select>
  15. </div>
  16. <hr>
  17. <div flag="mydiv" >
  18. <span>城市</span>
  19. </div>
  20. <hr>
  21. <!--内部插入-->
  22. <button onclick="append()">append</button>
  23. <button onclick="prepend()">prepend</button>
  24. <button onclick="appendto()">appendTo</button>
  25. <button onclick="prependto()">prependTo</button>
  26. <!--外部插入,平级关系-->
  27. <button onclick="after()">afetr</button>
  28. <button onclick="before()">before</button>
  29. <hr>
  30. <script type="text/javascript">
  31. // 在某元素结尾 插入指定内容 (内部)
  32. function append() {
  33. // $('div[flag="mydiv"]').append('摸牌');
  34. var hobby = $('#hobby').html();
  35. $('div[flag="mydiv"]').append(hobby);
  36. }
  37. // 在某元素开头 插入指定内容 (内部)
  38. function prepend() {
  39. var hobby = $('#hobby').html();
  40. $('div[flag="mydiv"]').prepend(hobby);
  41. }
  42. // 将指定内容 追加到某元素结尾 (内部)
  43. function appendto() {
  44. var hobby = $('#hobby').html();
  45. $('#part').appendTo( $('#hobby') );
  46. }
  47. // 将指定内容 追加到某元素开头 (内部)
  48. function prependto() {
  49. var hobby = $('#hobby').html();
  50. $('#part').prependTo( $('#hobby') );
  51. }
  52. // 在某元素结尾 插入指定内容 (外部)
  53. function after() {
  54. var html = '<select><option value="Look on">吃瓜</option><option value="listen to jokes">听笑话</option></select>'
  55. $('div[flag="mydiv"]').after(html);
  56. }
  57. // 在某元素开头 插入指定内容 (外部)
  58. function before() {
  59. var html = '<select><option value="Look on">吃瓜</option><option value="listen to jokes">听笑话</option></select>'
  60. $('div[flag="mydiv"]').before(html);
  61. }
  62. </script>

append

prepend

appendTo

prependTo

afetr

before

2、筛选、删除、克隆

  1. <!--筛选-->
  2. <button onclick="clones()">clone</button>
  3. <button name="btn" onclick="remove(this)">remove</button>
  4. <button name="btn" onclick="eq()">eq</button>
  5. <button name="btn" onclick="first()">first</button>
  6. <button name="btn" onclick="last()">last</button>
  7. <script type="text/javascript">
  8. // 删除
  9. function remove(obj) {
  10. $(obj).remove();
  11. }
  12. // 克隆
  13. function clones() {
  14. var btn = $('button[name="btn"]').clone();
  15. $('body').append( btn );
  16. }
  17. // 筛选删除
  18. function eq() {
  19. $('button[name="btn"]').eq(1).remove();
  20. }
  21. function first() {
  22. $('button[name="btn"]').first().remove();
  23. }
  24. function last() {
  25. $('button[name="btn"]').last().remove();
  26. }
  27. </script>

3、省市联动小案例

  1. <form action="">
  2. <label>籍贯</label>
  3. <select name="province" onchange="change_province()">
  4. <option value="">请选择省</option>
  5. <option value="Jiangsu">江苏</option>
  6. <option value="Zhejiang">浙江</option>
  7. <option value="Anhui">安徽</option>
  8. <option value="Shangdong">山东</option>
  9. </select>
  10. <label>城市</label>
  11. <select name="city">
  12. <option value="">请选择城市</option>
  13. </select>
  14. </form>
  15. <script type="text/javascript">
  16. function change_province() {
  17. var province = $('select[name="province"]').val();
  18. var citys = get_citys( province );
  19. var html = '<option value="">请选择城市</option>';
  20. for(var i=0; i<citys.length; i++){
  21. html += '<option value="' +citys[i].val+ '">'+citys[i].txt+'</option>';
  22. }
  23. $('select[name="city"]').empty().append( html ); // $().html(html)
  24. }
  25. function get_citys( province ) {
  26. var citys = [];
  27. if( province=='' ){
  28. return citys;
  29. }
  30. if( province=='Jiangsu' ){
  31. // 原则上是ajax后台获取
  32. citys = [{val:'Nanjing',txt:'南京'},{val:'Suzhou',txt:'苏州'}];
  33. }
  34. if( province=='Zhejiang' ){
  35. citys = [{val:'Hangzhou',txt:'杭州'},{val:'Wenzhou',txt:'温州'}];
  36. }
  37. if( province=='Shangdong' ){
  38. citys = [{val:'Jinan',txt:'济南'},{val:'qingdao',txt:'青岛'}];
  39. }
  40. if( province=='Anhui' ){
  41. citys = [{val:'Hefei',txt:'合肥'},{val:'Huangshan',txt:'黄山'}];
  42. }
  43. return citys;
  44. }
  45. </script>

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:作业完成的相当棒, 图文结合, 一看就懂
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