如何给div添加背景图片?

发布于 2024-09-13 18:47:00 字数 713 浏览 3 评论 0原文

我正在使用 Mojo SDK,并尝试向 div 添加背景图像,但尚未找到方法。 显然我已经尝试以正常的 CSS 方式进行操作,但它似乎不适用于 Mojo。

这是我尝试的最新方法:

            var t=document.getElementById("kblayoutdiv");
t.parentNode.removeChild(t);

var t=document.getElementsByTagName("BODY")[0];
var div=document.createElement("div");
div.style.position="fixed";
div.id="kblayoutdiv";
div.style.display="block";
div.style.top="80%";
div.style.left="92%";
div.style.width="16px";
div.style.height = "11px";
div.style.background = url('/usr/palm/frameworks/mojo/keyb_en-us.png');
div.style.zIndex = "255";
t.appendChild(div);

window.kblayout="en";

我尝试了几种解决方案来显示背景图像。 其余的工作正常。这只是背景图像,无论如何都不会显示。

因此,如果有人可以向我展示添加背景图像的代码片段,我会非常高兴:)

I am working with the Mojo SDK and I'am trying to add a background image to a div but havn't been able to find a way to do it.
Obviously I have tried doing it the normal CSS-way but it doesn't seem to work with Mojo.

Here is the latest thing I tried:

            var t=document.getElementById("kblayoutdiv");
t.parentNode.removeChild(t);

var t=document.getElementsByTagName("BODY")[0];
var div=document.createElement("div");
div.style.position="fixed";
div.id="kblayoutdiv";
div.style.display="block";
div.style.top="80%";
div.style.left="92%";
div.style.width="16px";
div.style.height = "11px";
div.style.background = url('/usr/palm/frameworks/mojo/keyb_en-us.png');
div.style.zIndex = "255";
t.appendChild(div);

window.kblayout="en";

I have tried several solutions to get the background image to show.
The rest is working fine. It is just the background iamge that won't show no matter what.

So if anyone can show me the piece of code to add the background image I'd be real happy :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2024-09-20 18:47:00

div.style.background = url('/usr/palm/frameworks/mojo/keyb_en-us.png');

您必须在 JavaScript 中引用字符串,

div.style.background = "url('/usr/palm/frameworks/mojo/keyb_en-us.png')";

显然,URI 也必须正确。

也就是说,根据经验,最好在外部样式表中预定义所有样式并生成与选择器匹配的元素(例如通过设置 .className)。

div.style.background = url('/usr/palm/frameworks/mojo/keyb_en-us.png');

You have to quote strings in JavaScript

div.style.background = "url('/usr/palm/frameworks/mojo/keyb_en-us.png')";

Obviously, the URI has to be correct as well.

That said, as a rule of thumb, it is almost always better to predefine all your styles in an external stylesheet and generate elements that match the selectors (e.g. by setting .className).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文