开发者

Randomly change background image

I'm working on a way to change the background pattern of the body on each click of a button.

I got that code so far, but when I click on the button, the background just turns white. Where is my mistake?

var patternImage = ['开发者_JAVA百科01','02','03','04'];
var randomNumber = Math.floor(Math.random() * patternImage.length);

$("#patternSwitcher").click(function(){
    $("body").css("background","url(../images/ + patternImage[randomNumber] + .gif)")
});


The string needs to be concatenated:

$("body").css("background","url(../images/" + patternImage[randomNumber] + ".gif)")


You want:

"string" + variable + "string"

Not:

"string + variable + string"

Since variables in JS are not flagged using symbols (such as $ in PHP), you can't interpolate variables in strings, you have to build them with concatenation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜