分享

JQuery简单学习(7)——jQuery HTML 操作

 kekokeko 2011-03-01

JQuery简单学习(7)——jQuery HTML 操作

文章分类:Web前端

 

jQuery 包含很多供改变和操作 HTML 的强大函数。

————————————————————
改变 HTML 内容
语法
Js代码 复制代码
  1. $(selector).html(content)  
 html() 函数改变所匹配的 HTML 元素的内容(innerHTML)。
实例
Js代码 复制代码
  1. <html>   
  2. <head>   
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>   
  4. <script type="text/javascript">   
  5. $(document).ready(function(){   
  6.   $("button").click(function(){   
  7.   $("p").html("W3School");   
  8.   });   
  9. });   
  10. </script>   
  11. </head>   
  12.   
  13. <body>   
  14. <h2>This is a heading</h2>   
  15. <p>This is a paragraph.</p>   
  16. <p>This is another paragraph.</p>   
  17. <button type="button">Click me</button>   
  18. </body>   
  19.   
  20. </html>  
 ————————————————————
添加 HTML 内容
语法
Js代码 复制代码
  1. $(selector).append(content)  
 append() 函数向所匹配的 HTML 元素内部追加内容。

语法
Js代码 复制代码
  1. $(selector).prepend(content)  
 prepend() 函数向所匹配的 HTML 元素内部预置(Prepend)内容。

实例
Js代码 复制代码
  1. <html>   
  2. <head>   
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>   
  4. <script type="text/javascript">   
  5. $(document).ready(function(){   
  6.   $("button").click(function(){   
  7.   $("p").append(" <b>W3School</b>.");   
  8.   });   
  9. });   
  10. </script>   
  11. </head>   
  12.   
  13. <body>   
  14. <h2>This is a heading</h2>   
  15. <p>This is a paragraph.</p>   
  16. <p>This is another paragraph.</p>   
  17. <button type="button">Click me</button>   
  18. </body>   
  19.   
  20. </html>  

 语法
Js代码 复制代码
  1. $(selector).after(content)  
 after() 函数在所有匹配的元素之后插入 HTML 内容。

语法
Js代码 复制代码
  1. $(selector).before(content)  
 before() 函数在所有匹配的元素之前插入 HTML 内容。

实例
Js代码 复制代码
  1. <html>   
  2. <head>   
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>   
  4. <script type="text/javascript">   
  5. $(document).ready(function(){   
  6.   $("button").click(function(){   
  7.   $("p").after(" W3School.");   
  8.   });   
  9. });   
  10. </script>   
  11. </head>   
  12.   
  13. <body>   
  14. <h2>This is a heading</h2>   
  15. <p>This is a paragraph.</p>   
  16. <p>This is another paragraph.</p>   
  17. <button type="button">Click me</button>   
  18. </body>   
  19.   
  20. </html>  
 ————————————————————
jQuery HTML 操作 - 来自本页
函数描述 $(selector).html(content) 改变被选元素的(内部)HTML $(selector).append(content) 向被选元素的(内部)HTML 追加内容 $(selector).prepend(content) 向被选元素的(内部)HTML “预置”(Prepend)内容 $(selector).after(content) 在被选元素之后添加 HTML $(selector).before(content) 在被选元素之前添加 HTML

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多