我正在用javascript创建一个按钮,并将其附加到HTML页面中
let dropDownButton = document.createElement('button');
dropDownButton.style.background = "url(/48.png)";
dropDownButton.style.width = size+"px";
dropDownButton.style.height = size+"px";
given.append(dropDownButton);
48.png
是我想要嵌入的本地文件。目前,这段代码在我声明的HTML页面中工作(在清单中创建和声明(,但在网页上不工作(例如twitter,最后一个空按钮是我附加的(。
为什么会发生这种情况?我可以修复它吗?还是直接返回到在按钮中插入img
元素?
等等,我修复了它。简单地将chrome.runtime.getURL
方法硬编码为url()
的语法以获得本地文件
所以不是
dropDownButton.style.background = "url(48.png)";
使用
dropDownButton.style.background = "url("+chrome.runtime.getURL('48.png')+")";