你好,游客 登录 注册 搜索
背景:
阅读新闻

jQuery动态添加和删除class属性

[日期:2012-06-13] 来源:Linux社区  作者:comeontom [字体:大 中 小]

在用jQuery时,遇到一个问题,就是如何动态得添加和删除class的属性,下面是我的解决代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>测试</title>  
  6. <script type="text/javascript" src="jquery-1.7.2.js"></script>  
  7. <script type="text/javascript">  
  8. $(document).ready(function(){  
  9.   $("button").click(function(){  
  10.       
  11.     $("p:first").addClass("intro");  
  12.     $("p:first").removeClass("default");  
  13.   });  
  14. });  
  15. </script>  
  16. <style type="text/css">  
  17. .intro  
  18. {  
  19. font-size:120%;  
  20. color:red;  
  21. }  
  22. .default  
  23. {  
  24.     font-size:16;  
  25.     color:#0F9;  
  26. }  
  27. </style>  
  28. </head>  
  29.   
  30. <body>  
  31. <h1>This is a heading</h1>  
  32. <p class="default">This is a paragraph.</p>  
  33. <p>This is another paragraph.</p>  
  34. <button>向第一个 p 元素添加一个类并删除一个类</button>  
  35. </body>  
  36. </html>  

效果如下:

点击前:

点击后:

linux
相关资讯       jQuery教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款