javascript怎么改变背景


本文摘自PHP中文网,作者青灯夜游,侵删。

javascript改变背景的方法:首先使用“document.getElementById('id值')”语句获取指定元素对象;然后使用“元素对象.style.background="背景颜色值或图片路径"”语句设置背景即可。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

javascript改变背景

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<style>

    #div{

        height: 200px;

        background: url(img/1.jpg) no-repeat;

    }

</style>

<script>

function displayResult(){

    document.getElementById('h1').style.background="paleturquoise";

    document.getElementById('div').style.background="url(img/2.jpg) no-repeat";

}

</script>

</head>

<body>

 

<h1 id="h1" style="background: red;">Hello World!</h1>

<div id="div"></div>

<br>

<button type="button" onclick="displayResult()">改变背景</button>

 

</body>

</html>

效果图:

阅读剩余部分

相关阅读 >>

javascript canvas方法有哪些

javascript中$是什么意思

javascript怎么绑定事件

javascript怎么去掉字符

javascript的内部方法有什么

分享javascript spread运算符的10种用法

聊聊javascript中eval()函数的用法

javascript如何引用到html中

javascript函数式编程中纯函数的理解(代码)

jquery wrap()方法是什么意思

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...