通过 Javascript 创建的图像未显示
我正在为 Firefox 编写一个扩展,将图像添加到当前网页。虽然这应该不是问题,但以下代码片段不起作用。我得到了一个50px/50px的矩形,即图像的边框,但图像本身没有显示。
var img = document.createElement("img");
img.style.border = "1px solid #000";
img.style.width = "50px";
img.style.height = "50px";
img.setAttribute("src", "chrome://myExtension/content/images/add-icon.png");
它也不适用于来自外部结果的图像。添加 div 环境似乎有效。 img.src="..." 不会改变任何东西。
我尝试将图像添加到 div 中。添加 div.length 后增加了 1。所以不知何故一切正常,除了图像没有显示。有什么提示吗?
谢谢并致以最诚挚的问候,
克里斯蒂安
I'm writing a extension for Firefox that adds images to the current web page. Although it shouldn't be a problem, the following code snippet doesn't work. I got a 50px/50px rectangle, i.e., the border of the image, but the image itself is not displayed.
var img = document.createElement("img");
img.style.border = "1px solid #000";
img.style.width = "50px";
img.style.height = "50px";
img.setAttribute("src", "chrome://myExtension/content/images/add-icon.png");
It also doesn't work with images from a external results. Adding div-environments seem to work. img.src="..." doesn't change anything.
I try to add the image to a div. After adding div.length has increased by 1. So somehow everything is working, except the image is not displayed. Any hints?
Thanks and best regards,
Christian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如评论中所讨论的,一致地使用“content.document”,而不是混合使用“content.document”和“document”。
As discussed in comments, use "content.document" consistently, instead of mixing "content.document" and "document".
您是否设置了内容可访问?
https://developer.mozilla.org/en/chrome_registration#contentaccessible
这是必需的顺序使非 chrome 内容中的 chrome 图像等可用。
Have you set contentaccessible?
https://developer.mozilla.org/en/chrome_registration#contentaccessible
It's required in order to make chrome images etc. available from non-chrome content.