简体   繁体   中英

Whats wrong with my Javascript?

I have this bit of javascript and php:

function change(){

var heading = "<?= $heading ?>";
var description = "<?= $description ?>";
var valid = "<?= $valid ?>";
var background = "<?= $background ?>";
alert("hi");
    switch(background){
        case 1:
        document.getElementById('main').style.backgroundImage = "(url"img/bg1.jpg")";
        break;

        case 2:

        break;

        case 3:

        break;

        default:
        break;
    }
}

If I remove the case 1, the line: document.getElementById('main').style.backgroundImage = "(url"img/bg1.jpg")"; the function will work, I'll get the hi alert. But if its in there it doesn't work at all. But I've used that line of code before and its worked, why does it cause this script to stop working?

TIA

尝试将该行替换为:

document.getElementById('main').style.backgroundImage = "url('img/bg1.jpg')";

case 1 should be as below

case 1:
    document.getElementById('main').style.backgroundImage = "url("img/bg1.jpg")";
    break;

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