JavaScript点击来回切换两个div
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .test1{ width:100px; height: 100px; background-color: #5C874A; } .test2{ width:100px; height: 100px; background-color: #66D9B1; } </style> <script type="text/javascript"> function myFunction(){ var div = document.getElementById('test1'); if(div.className.indexOf('test1') >= 0){ div.className='test2'; }else{ div.className='test1'; } } </script> </head> <body> <div class="test1" id="test1" alt="" onclick="myFunction()"> </div> </body> </html>
这个方法解决了上一篇中无法读取识别十六进制颜色代码的问题。
-Abai.