简体   繁体   中英

JavaScript Error: Unexpected Identifier

I have a script that performce this action:

document.getElementById("bildfram2").style.backgroundImage = "url("bilder[count]")";

I have an array looking like this:

var bilder = ["../img/startsida/nature1.jpg","../img/startsida/nature2.jpg","../img/startsida/nature3.jpg","../img/startsida/nature4.jpg"];

I get this error from the first line of code:

Uncaught SyntaxError: Unexpected identifier

Can someone help me fix the issue! Thanks!

You need to concatenate a string and a variable with +

document.getElementById("bildfram2").style.backgroundImage = 
    "url(" + bilder[count] + ")";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM