I have a button that I want to change the current background but can not figure out how to make javascript know what my class is for my background picture which is set in css. I found a way by changing the class to just header but thats not really what I wanna do. sry for sloppy coding.
<script type="text/javascript">
function ps() {
var bg = document.getElementsByTagName('headBack')[0];
bg.style.backgroundImage= 'url(background.jpg)';
}
</script>
<header class="headBack">
</header>
<div class="slideButtons">
<ul>
<li class="active">
<button class="button" onclick="ps()"></button>
</li>
</ul>
my css
.headBack {
background-image: url(background2.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: top center;
height: 600px;
}
You need to use document.getElementsByClassName
not document.getElementsByTagName
for your requirement
function ps() {
var bg = document.getElementsByClassName('headBack')[0];
bg.style.backgroundImage= 'url(background.jpg)';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.