如何使用javascript改变div的背景图片? [英] how to change the background image of div using javascript?

查看:193
本文介绍了如何使用javascript改变div的背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

        <div style="text-align:center;">
            <div class="ghor" id="a" onclick="chek_mark()">
            </div>

函数调用

        <script type="text/javascript">

            function chek_mark(){
                var el= document.getElementById("a").style.background-    image;

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

here i want to change the background image using if else condition

if (el.url("Black-Wallpaper.jpg"))  
                    {                                  
                        el.url = "cross1.png";  
                    }
                    else if(el.url("cross1.png"))
                    {

                      alert("<h1>This is working too.</h1>");
                    }
                }

这是样式表

.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'的背景图片, '

i want change the background image of the 'div' which class is 'ghor'

推荐答案

尝试:

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

希望它有帮助!

这篇关于如何使用javascript改变div的背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆