1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .root{ background-color: salmon; } </style> </head> <body> <div>div1</div> <div>div2</div> <div>div3</div> <div>div4</div> <button id="btn">点击切换样式</button> </body> </html> <script src="js/jquery-3.5.1.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript"> var div1 = document.querySelector("div:first-child"); div1.className = "c1"; $("div").addClass("one"); $("div").addClass(function(index, value){ console.log(index, value); return "div" + (index + 1); }); var classArray = ["root", "box", "header", "content"]; $("div").addClass(function(index, value){ return classArray[index]; }) $("div").removeClass("c1"); $("div").removeClass("one"); $("div").removeClass(function(index, value){ return "div" + (index + 1); }) $("div").removeClass(function(index, value){ return classArray[index]; }) $("#btn").click(function(){ $("div").toggleClass("root"); })
</script>
|