简体   繁体   English

如何将背景图像添加到div?

[英]How to add background-image to a div?

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. 我正在使用Mojo SDK,并且正在尝试将背景图像添加到div中,但是还没有找到一种方法来实现。 Obviously I have tried doing it the normal CSS-way but it doesn't seem to work with Mojo. 显然,我已经尝试过以普通的CSS方式进行操作,但是它似乎不适用于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 :) 因此,如果有人可以向我展示一段添加背景图像的代码,我将非常高兴:)

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

You have to quote strings in JavaScript 您必须在JavaScript中引用字符串

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

Obviously, the URI has to be correct as well. 显然,URI也必须正确。

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 (eg by setting .className ). 也就是说,根据经验,最好总是在外部样式表中预定义所有样式并生成与选择器匹配的元素(例如,通过设置.className )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM