开发者

set background image of a css element

开发者 https://www.devze.com 2023-01-08 14:16 出处:网络
i use the following to to set the text content of an css element var cell = document.createElement(\'li\');

i use the following to to set the text content of an css element

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??开发者_StackOverflow社区


$(cell).css("background-image","url('mybg.png)");


You can use the "map version" of jQuery css method:

$(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.


Use addClass. I think it is less verbose, more efficient, and prettier, plus it is better (i.e. more maintainable) to keep your style definitions outside of your implementation:

.prettyWithImage { background-image: url(/someimage.jpg); color: red }

$(cell).addClass('prettyWithImage');


Using jQuery:

$(cell).css('background-image', 'url(image-location.jpg)').css('color', '#ABCDEF');
0

精彩评论

暂无评论...
验证码 换一张
取 消