3 回答

TA贡献1815条经验 获得超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条经验 获得超7个赞
您可以动态删除样式
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条经验 获得超8个赞
如果使用样式标签而不是单独的文件没有问题,您可以添加 id 并根据需要操作它们。
HTML:
<style id='style1'>color: red;</style>
<style id='style2'>color: red;</style>
Javascript:
const styleSheet = document.getElementById("#style1");
remove(styleSheet);
添加回答
举报