如何使用javascript更改div的背景图像?

IT技术 javascript html css
2021-02-20 02:37:30

这是我的代码

<div style="text-align:center;">
  <div class="ghor" id="a" onclick="chek_mark()"></div>
  function call
</div>
<script type="text/javascript">
  function chek_mark(){
   var el= document.getElementById("a").style.background-image;
   if (el.url("Black-Wallpaper.jpg"))  
   {
     el.url = "cross1.png";
    }
    else if(el.url("cross1.png"))
    {
      alert("<h1>This is working too.</h1>");
     }
   }
</script>

在这里我想使用 if else 条件更改背景图像

this is the style-sheet
.ghor //this is the div class
{
    background-image: url('Black-Wallpaper.jpg');
    background-size: cover;
    border-radius: 5px;
    height: 100px;
    width: 100px;
    box-shadow: 2px 5px 7px 7px white;
    /*background-color: black;*/
    display:inline-block; 
}

我想更改“div”的背景图像,哪个类是“ghor”

6个回答

试试这个:

document.getElementById('a').style.backgroundImage="url(images/img.jpg)"; // specify the image path here

希望能帮助到你!

试试这个!

var el = document.getElementById("a").style.backgroundImage;
if(el == "url(Black-Wallpaper.jpg)") { // full value is provided
   el.style.backgroundImage = "url(/link/to_new_file.png)"; // change it
}
'(/link/to_new_file.png)' 我应该在这里写什么
2021-04-22 02:37:30
它不起作用'var el = document.getElementById('a').backgroundImage; if (el == "url(Black-Wallpaper.jpg)") { //el.style.backgroundImage = "url(cross1.png)"; 警报(“它的工作”);}'
2021-04-25 02:37:30
请添加('a').style.backgrounImage而不是简单的('a').backgroundImage. 因为背景是对象的样式属性而不是对象的属性。
2021-04-28 02:37:30
这只会获得文件名,还是也会获得路径?
2021-05-01 02:37:30
@Avisari,它将获取background-imagecss 属性的值如果提供了文件路径,其值也将包括文件路径。否则只有图像!
2021-05-14 02:37:30

您可以通过以下方式进行

步骤1

   var imageUrl= "URL OF THE IMAGE HERE";
   var BackgroundColor="RED"; // what ever color you want

用于改变BODY 的背景

document.body.style.backgroundImage=imageUrl  //changing bg image
document.body.style.backgroundColor=BackgroundColor //changing bg color

更改带有 ID 的元素

document.getElementById("ElementId").style.backgroundImage=imageUrl
document.getElementById("ElementId").style.backgroundColor=BackgroundColor 

对于具有相同类的元素

   var elements = document.getElementsByClassName("ClassName")
        for (var i = 0; i < elements.length; i++) {
            elements[i].style.background=imageUrl;
        }

HTML

<body id="body">
</body>

JavaScript

你也可以设置时间。

//Initializing
var i = 0;
var images = []; //array
var time = 3000; // time in millie seconds

//images

images[0] = "url(./Images/1.jpg)";
images[1] = "url(./Images/2.jpg)";
images[2] = "url(./Images/3.jpg)";
//function

function changeImage() {
    var el = document.getElementById('body');
    el.style.backgroundImage = images[i];
    if (i < images.length - 1) {
        i++;
    } else {
        i = 0;
    }
    setTimeout('changeImage()', time);
}

window.onload = changeImage;

CSS

overflow-x: hidden;
margin: 0;
padding: 0;
width: 100%;
height: 100vh;
background-repeat: no-repeat;
background-position: center;
请添加解释为什么会这样。谢谢!
2021-04-26 02:37:30

    

 //Here is an example how to change background image of <div> element using javascript  by feching a new URL form some API.
 // let image teched from API: this is the image you want to put as new background !
// thumbnail image:
// data.picture.thumbnail
//the HTML element whose background needs to be changes is given an id of #thumbnail.

 var thumbUrl = document.querySelector("#thumbnail");
     
//using a function which will update data in the HTML - this is done
//after parsing of the data fetched, I have omitted the steps used for parsing the data.
function updateData(data){
   //data.picture.medium represents our new fetched data containing the URL fo the new image we want to use as background image.
    var newImage=data.picture.medium;
    thumbUrl.style.backgroundImage="url("+newImage+")";
}