Reference code implements: https://highlightjs.org/
The principle, based on the link tags disable.
Demo code:
index.html
<!DOCTYPE /> <html> <head> <meta charset="utf-8"> <title>setTimeout</title> <link class="codestyle" rel="prefetch alternate stylesheet" href="./c1.css"> <link class="codestyle" rel="prefetch alternate stylesheet" href="./c2.css"> <script type="text/javascript"> //link 切换 var style = 'c1'; function initSnippet() { for (let link of document.querySelectorAll('.codestyle')) { link.rel = 'stylesheet'; link.disabled = !link.href.match(style + '\\.css$');// regex. "" Needs to be escaped, add "\", "\" and needs to be escaped the setTimeout (function () { } } The addEventListener ( 'Load', initSnippet); style = "c2"; initSnippet(); }, 1000) </script> </head> <body> <div style="width: 100px;height: 100px;"></div> </body> </html>
c1.css
div { background-color: #f00; }
c2.css
div { background-color: #0f0; }