菜单切换
FACESOHO知行者
心灵
记录
远方
赞赏工具
源代码:
点击运行
保存
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>小鸟启蒙(facesoho.com)</title> <style> .mystyle { width:500px; height:50px; border:1px solid black; } .anotherClass { background-color:lightblue; padding:25px; } .thirdClass { text-align:center; font-size:25px; color:navy; margin-bottom:10px; } </style> </head> <body> <p>点击按钮查看 DIV 元素是否有 "mystyle" 类,如果有移除 "anotherClass" 类。</p> <div id="myDIV" class="mystyle anotherClass thirdClass"> 我是一个DIV元素 </div> <button onclick="myFunction()">点我</button> <p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myDIV"); if (x.classList.contains("mystyle")) { x.classList.remove("anotherClass"); } else { alert("不存在该类。"); } } </script> </body> </html>
运行结果