首页 > 语言 > JavaScript > 正文

jQuery给多个不同元素添加class样式的方法

2024-05-06 16:17:42
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了jQuery给多个不同元素添加class样式的方法,实例分析了addClass()方法添加样式的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了jQuery给多个不同元素添加class样式的方法。分享给大家供大家参考。具体分析如下:

jQuery可以通过addClass()方法给多个不同的html元素同时添加相同的class

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <script src="js/jquery.min.js"> 
  5. </script> 
  6. <script> 
  7. $(document).ready(function(){ 
  8. $("button").click(function(){ 
  9. $("h1,h2,p").addClass("blue"); 
  10. $("div").addClass("important"); 
  11. }); 
  12. }); 
  13. </script> 
  14. <style type="text/css"> 
  15. .important 
  16. { 
  17. font-weight:bold; 
  18. font-size:xx-large; 
  19. } 
  20. .blue 
  21. { 
  22. color:blue; 
  23. } 
  24. </style> 
  25. </head> 
  26. <body> 
  27. <h1>Heading 1</h1> 
  28. <h2>Heading 2</h2> 
  29. <p>This is a paragraph.</p> 
  30. <p>This is another paragraph.</p> 
  31. <div>This is some important text!</div> 
  32. <br> 
  33. <button>Add classes to elements</button> 
  34. </body> 
  35. </html> 

希望本文所述对大家的jQuery程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选