[英]set background image of a css element
i use the following to to set the text content of an css element 我使用以下内容来设置css元素的文本内容
var cell = document.createElement('li');
cell.textContent = this.labelForIndex(index);
Now i want to set the back-ground image and color.....how to do it?? 现在我想设置背景图像和颜色.....怎么做?
$(cell).css("background-image","url('mybg.png)");
Use addClass
. 使用
addClass
。 I think it is less verbose, more efficient, and prettier, plus it is better (ie more maintainable) to keep your style definitions outside of your implementation: 我认为它更简洁,更高效,更漂亮,而且更好(即更易于维护)将样式定义保留在实现之外:
.prettyWithImage { background-image: url(/someimage.jpg); color: red }
$(cell).addClass('prettyWithImage');
You can use the "map version" of jQuery css method: 您可以使用jQuery css方法的“地图版本”:
$(cell).css({'background-image': 'url("your-bg.png")',
'background-color': '#abcdef'});
But if you can (if background image and color is always the same for instance) use addClass
as karim79 told you to. 但是,如果可以(例如背景图像和颜色总是相同)使用
addClass
作为karim79告诉你。
使用jQuery:
$(cell).css('background-image', 'url(image-location.jpg)').css('color', '#ABCDEF');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.