使用 chrome 扩展程序,我想更改与单个 div 关联的样式表。
例如,当前页面看起来像:
<div id="footer">
<link rel="stylesheet" type="text/css" href="linktostylesheet.css">
...
</div>
我不想使用那个样式表,而是插入我自己的样式表:
<div id="footer">
<link rel="stylesheet" type="text/css" href="link to my new stylesheet.css">
...
</div>
我对这种类型的编程有点迷茫。到目前为止,我的内容脚本中有以下代码片段,但我认为我的想法不正确。
d = document.getElementById("footer").getContentDocument();
d.styleSheets[d.styleSheets.length].href = 'newstylesheet.css';
最佳答案
<link>
元素应该是 <head>
的子元素元素。您可以选择 link
元素的起始值为 href
属性,设置 href
元素到新值。
document.querySelector("link[href^=linktostylesheet]").href = "newstylesheet.css";
关于javascript - 使用 chrome 扩展更改 div 中的样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42050197/