3 回答

TA貢獻1815條經(jīng)驗 獲得超10個贊
嘗試僅在加載后添加樣式表:
//document.addEventListener('DOMContentLoaded', ...
window.addEventListener('load', function(){
var link = document.createElement('link');
link.rel = "stylesheet";
link.href = "Styles2.css";
document.querySelector('head').appendChild( link );
});

TA貢獻1865條經(jīng)驗 獲得超7個贊
您可以動態(tài)刪除樣式
window.addEventListener('load', function () {
const path = '[pathto]/Styles2.css';
const styles = document.querySelectorAll('link');
const style2 = Array.from(styles).find(k => k.href === path);
const head = document.getElementsByTagName('head')[0];
head.removeChild(style2);
});

TA貢獻1898條經(jīng)驗 獲得超8個贊
如果使用樣式標簽而不是單獨的文件沒有問題,您可以添加 id 并根據(jù)需要操作它們。
HTML:
<style id='style1'>color: red;</style>
<style id='style2'>color: red;</style>
Javascript:
const styleSheet = document.getElementById("#style1");
remove(styleSheet);
添加回答
舉報