gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 12:17:19 25 4
gpt4 key购买 nike

这是我的代码

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

我想改变'ghor'类的'div'的背景图片

最佳答案

试试这个:

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

希望对您有所帮助!

关于javascript - 如何使用javascript更改div的背景图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com