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会怎样

javascript如何定义私有方法

vue-cli 3.x 自动部署项目至服务器的方法介绍(代码)

es6箭头函数与function有什么区别?

学习使用javascript实现utf-8编解码

typescript和javascript的区别有哪些

javascript创建对象的方法有哪些

学会html能做什么工作

js同源策略是什么

简单对比,看看typescript中interface和type间的区别

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




打赏

取消

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

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

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

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

评论

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