开发者

Change Image SRC on condition

Hey guys, I'm trying to 开发者_如何学Cdisplay an image depending on a variable that's in my HTML. Let me explain further...

I have this HTML

<div class="container">
<span class="var">1</span>
<img class="varimg" src="" alt="" />
</div>

And I want jQuery to analyse the span.var text, then if it is:

1: return "images/1.jpg"

2: return "images/2.jpg"

3: return "images/3.jpg"

(there will be only 3...)

and then inject it in the img src...

Is it possible ? I've been trying but I'm still a newbie in jQuery...

Thanks a lot in advance !


$('.varimg').each(function() {
   $(this).attr('src','images/'+$(this).prev().text()+'.jpg');
});

put this code in your $(document).ready()

2nd edit: fixed it! click here for a jsfiddle demo :)


try this..

imgSrc = 'images/'+$(".var").text()+'.jpg');
$('.varimg').attr('src',imgSrc );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜