最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - set background image of a css element - Stack Overflow

programmeradmin5浏览0评论

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??

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??

Share Improve this question asked Jul 25, 2010 at 13:06 RonyRony 1,2294 gold badges22 silver badges42 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4
$(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)

  1. 暂无评论