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

bt1cpqcv  于 2023-04-04  发布在  Java
关注(0)|答案(9)|浏览(175)

这是我的密码

<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'

4xy9mtcn

4xy9mtcn1#

我也遇到了同样的问题🥲。然后我意识到,当元素的位置是固定的或绝对的时,JavaScript不能正常工作。尝试将其位置更改为相对位置或删除位置属性。

jpfvwuh4

jpfvwuh42#

你可以使用Jquery

$('#div_id').css("backgroundImage","url(img_path)");
dhxwm5r4

dhxwm5r43#

试试这个:

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

希望有帮助!

a1o7rhls

a1o7rhls4#

试试这个!

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
}
qco9c6ql

qco9c6ql5#

你可以用以下方法来做

第一步

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;
        }
z6psavjg

z6psavjg6#

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;
xhv8bpkk

xhv8bpkk7#

//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+")";
}
y0u0uwnf

y0u0uwnf8#

//这里是一个例子,如何改变背景图片的元素使用javascript通过feching一个新的URL形式一些API。
//从API获取图片:这是你想把作为新背景的图像!
// thumbnail image:
// data.picture.thumbnail
//需要改变背景的HTML元素的id为#thumbnail。

var thumbUrl = document.querySelector("#thumbnail");

//使用一个函数来更新HTML中的数据-这就完成了
//解析获取的数据后,我省略了解析数据的步骤。

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+")";

}
zzwlnbp8

zzwlnbp89#

你能做到的
背景图片:url(${musics[0].image})

相关问题