Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 改变身体背景_Javascript_Html_Css - Fatal编程技术网

Javascript 改变身体背景

Javascript 改变身体背景,javascript,html,css,Javascript,Html,Css,我想在单击按钮时更改我身体的背景图像 我的html代码是 <body id = "body"> <input type = "button" id = "button" value = "Change Background" onclick = "Background_change()"></input> </body> Java脚本 我尝试使用下面的代码使用JavaScript函数更改图像 function Background_

我想在单击按钮时更改我身体的背景图像

我的html代码是

<body id = "body">

    <input type = "button" id = "button" value = "Change Background" onclick  = "Background_change()"></input>

</body>
Java脚本

我尝试使用下面的代码使用JavaScript函数更改图像

function Background_change()
{
    var imager = document.getElementById("body").style.backgroundImage;
    imager.style.backgroundImage = url("20a.jpg");
}
您需要:

  • 引用元素,而不是样式属性
  • 在值之前和之后使用引号
    '
  • 如下更改代码:

    function Background_change()
    {
        var imager = document.getElementById("body");
        imager.style.backgroundImage = 'url("20a.jpg")';
    }
    
    试试这个:

  • 首先得到body的变量
  • 然后给出带有“或”的图像链接
    function Background_change(){
    var imager=document.getElementById(“主体”);
    imager.style.backgroundImage=“url(http://im.rediff.com/money/2013/may/06car5.jpg)";
    }
    
    
    当用户
    单击
    按钮上的
    输入
    处于
    焦点时,会发生这种情况?你能解释一下吗?只尝试
    var imager=document.getElementById(“body”)
    而不尝试
    .style.backgroundImage
    
    document.body.style.backgroundImage = "url('yourNewImage.jpg')"
    
    function Background_change()
    {
        var imager = document.getElementById("body");
        imager.style.backgroundImage = 'url("20a.jpg")';
    }