怎么通过原生JS改变元素的class属性
解决方法:document.getElementById('test').className = 'emphasis'
Eg:
<!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Test</title> <style type="text/css"> p { color: #000000; /* black */ } p.emphasis { color: #cc0000; /* red */ } </style> </head> <body> <p id="test">Test</p> <hr> <input type="button" value="修改className为emphasis" onclick="document.getElementById('test').className = 'emphasis';"> <input type="button" value="修改className为空" onclick="document.getElementById('test').className = '';"> </body> </html>