开发者

How to build a string with the value of a variable embedded?

开发者 https://www.devze.com 2022-12-09 14:05 出处:网络
HI, I\'ve 开发者_开发问答got: <script language=\"javascript\" type=\"text/javascript\"> $(document).ready(function() {

HI, I've 开发者_开发问答got:

<script language="javascript" type="text/javascript">
$(document).ready(function() {
 $("#thumbs a").click( function() {
      var BGswitch = $(this).attr("href");
      $("#target").css("background-image", "url(BGswitch)");
      return false;
      });
});
</script>

And it's not quite working--when I look at the code, the variable itself (BGswitch) is being put in the background-image slot instead of the value of the variable which is supposed to be the href.... Anyone? Some kind of syntax error or something?

THANK YOU!


$("#target").attr("background-image", "url(" + BGswitch + ")");

Check this


"url(BGswitch)" is literally the string "url(BGswitch)". You need to build the string by concatenation:

"url(" + BGswitch + ")"


Shouldn't it be

$("#target").css("background-image", "url("+BGswitch+")");
0

精彩评论

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