如何动态设置背景图像?

时间:2014-09-17 05:37:54

标签: jquery dynamic background src

我的页面中有一个div = class =" post-first&#34 ;,在这个div中有多个图像,我不知道它们可能是什么!我只是想当我在这个div上盘旋时,第一张图片出现在background.below显示我做了什么。没有回答!!感谢您的帮助。



$(document).ready(function(e) {
    $('.post-first').mouseover(function(){
		var a=$('img')[0].src;
		alert(a);
		$(this).css('background-image','url(a)');
		$(this).css('background-color','#F00')
		})
});

.post-first{
	background-color:#039;
	display:inline-block;
	height:13em;
	margin-left:4px;
	padding:4px
}
div.post-first img{
	opacity:0
	}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<div class="post-first">
<h3>[cb:post_title]</h3><br />
[cb:post_body1]
<img src="http://ts1.mm.bing.net/th?id=HN.607986701601669353&pid=1.7"/>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您需要连接变量值,它当前被评估为字符串并呈现为:

background-image: url("a")

将其更改为:

$(this).css('background-image', "url('"+a+"')");

FIDDLE:

http://jsfiddle.net/wps7bn5s/