使用Javascript更改背景

使用Javascript更改背景,javascript,css,Javascript,Css,我正在尝试使用外部javascript文件中的函数更改div的背景 这是div的css: 以及应改变背景的功能: 检查表单后调用该函数: 我还尝试: document.getElementById("astronaut").style.background = "url(Junge_InRakete.png)"; 只有双引号。全局变量也可以。有人有线索吗?试试下面这句话 document.getElementById("astronaut").style.backgroundImage

我正在尝试使用外部javascript文件中的函数更改div的背景

这是div的css: 以及应改变背景的功能:
检查表单后调用该函数:
我还尝试:

 document.getElementById("astronaut").style.background = "url(Junge_InRakete.png)";
只有双引号。全局变量也可以。有人有线索吗?

试试下面这句话

 document.getElementById("astronaut").style.backgroundImage = "url('Junge_InRakete.png')";
我用过一次:

<script type="text/javascript"> 
    function changeBackdrop(toWhat) {
        document.getElementById("backDrop").style.background = "url('images/" + toWhat + ".jpg')";
    }
    changeBackdrop("bg_blueberries");
</script>

功能更改背景(toWhat){
document.getElementById(“background”).style.background=“url('images/”+toWhat+”.jpg')”;
}
变化背景(“bg_蓝莓”);
您应该使用:

document.getElementById("astronaut").style.backgroundImage = "url('Jungle_InRakete.png')";

虽然它与背景相似,但现在它可以与背景图像一起工作。非常感谢。
 document.getElementById("astronaut").style.backgroundImage = "url('Junge_InRakete.png')";
<script type="text/javascript"> 
    function changeBackdrop(toWhat) {
        document.getElementById("backDrop").style.background = "url('images/" + toWhat + ".jpg')";
    }
    changeBackdrop("bg_blueberries");
</script>
document.getElementById("astronaut").style.backgroundImage = "url('Jungle_InRakete.png')";