Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

javascript - 如何使用javascript更改div的背景图像?(how to change the background image of div using javascript?)

this is my code(这是我的代码)

<div style="text-align:center;"> <div class="ghor" id="a" onclick="chek_mark()"> </div> function call(函数调用) <script type="text/javascript"> function chek_mark(){ var el= document.getElementById("a").style.background- image; here i want to change the background image using if else condition(在这里我想使用if条件更改背景图像) if (el.url("Black-Wallpaper.jpg")) { el.url = "cross1.png"; } else if(el.url("cross1.png")) { alert("<h1>This is working too.</h1>"); } } 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; } i want change the background image of the 'div' which class is 'ghor'(我想更改“ div”类别为“ ghor”的背景图像)   ask by shajib0o translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:(尝试这个:)

document.getElementById('a').style.backgroundImage="url(images/img.jpg)"; // specify the image path here Hope it helps!(希望能帮助到你!)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...