如何更改HTML背景JavaScript函数? [英] How to change HTML background with JavaScript Function?

查看:243
本文介绍了如何更改HTML背景JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改HTML背景JavaScript函数?我需要一些简单的函数来从一个图像改变背景到另一个?

How to change HTML background with JavaScript Function? I need some simple function to change background from one image to another?

推荐答案

很简单是这样的:

function changeBGImage(){
     document.body.background = "image.jpg";
}

希望这有助于你

更新

或者,如果你想使用CSS(更优雅的解决方案)有了它,你可以使用:

Or if you wanna use CSS (more elegant solution) with it, you could use:

<style>
.bg1 {
background-image: url(images/my_image.jpg); 
}
</style>

<body id="page_body">

<script>
function changeBGImage(whichImage){
     document.getElementById('page_body').className="bg"+whichImage;
}
</script>

这篇关于如何更改HTML背景JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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