簡體   English   中英

更改正文背景

[英]Change body Background

我想在單擊按鈕時更改身體的背景圖像。

我的html代碼讓是

<body id = "body">

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

</body>

我的CSS代碼是

body {
    background-image : url('20a.jpg');
    font: 18px Arial ;
    color : white;
}

Java腳本

我嘗試使用以下代碼使用JavaScript函數更改圖像

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

你需要:

  1. 引用元素,而不是style屬性。
  2. 在值之前和之后使用引號'

像這樣更改代碼:

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

嘗試這個:

  1. 首先獲取身體的變量
  2. 然后用'或“”給出圖像的鏈接。

 function Background_change(){ var imager = document.getElementById("body"); imager.style.backgroundImage = "url(http://im.rediff.com/money/2013/may/06car5.jpg)"; } 
 <body id="body"> <input type="button" id="button" value="Change Background" onclick="Background_change()"></input> </body> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM