分享

6月8号工作(jQuery

 一夜梨花开 2014-07-30
今天的工作

1、append() 方法

Html5代码 复制代码 收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">  
  5. </script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.   $("#btn1").click(function(){  
  9.     $("p").append(" <b>Appended text</b>.");  
  10.   });  
  11.   
  12.   $("#btn2").click(function(){  
  13.     $("ol").append("<li>Appended item</li>");  
  14.   });  
  15. });  
  16. </script>  
  17. </head>  
  18.   
  19. <body>  
  20. <p>This is a paragraph.</p>  
  21. <p>This is another paragraph.</p>  
  22. <ol>  
  23. <li>List item 1</li>  
  24. <li>List item 2</li>  
  25. <li>List item 3</li>  
  26. </ol>  
  27. <button id="btn1">追加文本</button>  
  28. <button id="btn2">追加列表项</button>  
  29. </body>  
  30. </html>  


append() 方法可以将原本没有的html通过jquery加进去只要使用和前面同样的class就可以在添加的瞬间完成style的设置

2、prepend() 方法

Html5代码 复制代码 收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.   $("#btn1").click(function(){  
  8.     $("p").prepend("<b>Prepended text</b>. ");  
  9.   });  
  10.   $("#btn2").click(function(){  
  11.     $("ol").prepend("<li>Prepended item</li>");  
  12.   });  
  13. });  
  14. </script>  
  15. </head>  
  16. <body>  
  17.   
  18. <p>This is a paragraph.</p>  
  19. <p>This is another paragraph.</p>  
  20. <ol>  
  21. <li>List item 1</li>  
  22. <li>List item 2</li>  
  23. <li>List item 3</li>  
  24. </ol>  
  25.   
  26. <button id="btn1">添加文本</button>  
  27. <button id="btn2">添加列表项</button>  
  28.   
  29. </body>  
  30. </html>  


prepend() 方法跟前面的元素添加是一样的效果但是一样的在于在面的方法是在默认的到最后进行添加但是prepend() 方法是可以在最前面开始进行添加

3、remove() 方法

Html5代码 复制代码 收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.   $("button").click(function(){  
  8.     $("#div1").remove();  
  9.   });  
  10. });  
  11. </script>  
  12. </head>  
  13.   
  14. <body>  
  15.   
  16. <div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">  
  17. This is some text in the div.  
  18. <p>This is a paragraph in the div.</p>  
  19. <p>This is another paragraph in the div.</p>  
  20. </div>  
  21.   
  22. <br>  
  23. <button>删除 div 元素</button>  
  24.   
  25. </body>  
  26. </html>  


remove() 方法可以把指定的元素通过remove() 方法给删除掉在那里面就是把整个div给全部删除掉

4、empty() 方法

Html5代码 复制代码 收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.   $("button").click(function(){  
  8.     $("#div1").empty();  
  9.   });  
  10. });  
  11. </script>  
  12. </head>  
  13.   
  14. <body>  
  15.   
  16. <div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">  
  17. This is some text in the div.  
  18. <p>This is a paragraph in the div.</p>  
  19. <p>This is another paragraph in the div.</p>  
  20. </div>  
  21.   
  22. <br>  
  23. <button>清空 div 元素</button>  
  24.   
  25. </body>  
  26. </html>  


empty() 方法同样是删除但是不一样的事前者是全部删除连div自身一起删除但是empty() 方法不一样empty() 方法是清空意思就是可以把指定的div里面的内容全部清空但是会保留下当前的div

5、addClass() 方法

Html5代码 复制代码 收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">  
  5. </script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.   $("button").click(function(){  
  9.     $("#div1").addClass("important blue");  
  10.   });  
  11. });  
  12. </script>  
  13. <style type="text/css">  
  14. .important  
  15. {  
  16. font-weight:bold;  
  17. font-size:xx-large;  
  18. }  
  19. .blue  
  20. {  
  21. color:blue;  
  22. }  
  23. </style>  
  24. </head>  
  25. <body>  
  26.   
  27. <div id="div1">这是一些文本。</div>  
  28. <div id="div2">这是一些文本。</div>  
  29. <br>  
  30. <button>向第一个 div 元素添加类</button>  
  31.   
  32. </body>  
  33. </html>  


addClass() 方法是可以通过jquery直接向一个没有class的标签里面添加进入一个带有属性的class在里面在点击按钮以后原来的第二行文字会发生变化对应$("#div1").addClass("important blue");这条语句的意思是变成蓝色加大

6、removeClass() 方法

Html5代码 复制代码 收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.   $("button").click(function(){  
  8.     $("h1,h2,p").removeClass("blue");  
  9.   });  
  10. });  
  11. </script>  
  12. <style type="text/css">  
  13. .important  
  14. {  
  15. font-weight:bold;  
  16. font-size:xx-large;  
  17. }  
  18. .blue  
  19. {  
  20. color:blue;  
  21. }  
  22. </style>  
  23. </head>  
  24. <body>  
  25.   
  26. <h1 class="blue">标题 1</h1>  
  27. <h2 class="blue">标题 2</h2>  
  28. <p class="blue">这是一个段落。</p>  
  29. <p>这是另一个段落。</p>  
  30. <br>  
  31. <button>从元素上删除类</button>  
  32. </body>  
  33. </html>  


removeClass() 方法是可以将原本字体的蓝色给删除掉不是改变是删除掉

7、toggleClass() 方法

Html5代码 复制代码 收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">  
  5. </script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.   $("button").click(function(){  
  9.     $("h1,h2,p").toggleClass("blue");  
  10.   });  
  11. });  
  12. </script>  
  13. <style type="text/css">  
  14. .blue  
  15. {  
  16. color:blue;  
  17. }  
  18. </style>  
  19. </head>  
  20. <body>  
  21.   
  22. <h1>标题 1</h1>  
  23. <h2>标题 2</h2>  
  24. <p>这是一个段落。</p>  
  25. <p>这是另一个段落。</p>  
  26. <button>切换 CSS 类</button>  
  27. </body>  
  28. </html>  


toggleClass() 方法有着上面两种效果在没有蓝色的字体上加上蓝色在有蓝色的字体上清除蓝色

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约