javascript怎么改变背景
时间:2021-07-03 来源:互联网
今天PHP爱好者给大家带来javascript改变背景的方法:首先使用“document.getElementById('id值')”语句获取指定元素对象;然后使用“元素对象.style.background="背景颜色值或图片路径"”语句设置背景即可。希望对大家有所帮助。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
javascript改变背景
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
#p{
background: url(img/1.jpg) no-repeat;
}
</style>
<script>
function displayResult(){
document.getElementById('h1').style.background="paleturquoise";
document.getElementById('p').style.background="url(img/2.jpg) no-repeat";
}
</script>
</head>
<body>
<h1 id="h1" style="background: red;">Hello World!</h1>
<p id="p"></p>
<br>
<button type="button" onclick="displayResult()">改变背景</button>
</body>
</html>
效果图:
说明:
getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用。
如果没有指定 ID 的元素返回 null
如果存在多个指定 ID 的元素则返回第一个。
background 属性在一个声明中设置所有的背景属性。
设置 background 属性的语法:
Object.style.background="color image repeat attachment position"
以上就是javascript怎么改变背景的详细内容,更多请关注php爱好者其它相关文章!
-
PEPE币目前在哪些平台支持合约杠杆?最大倍数是多少 2025-06-30
-
ORDI币在哪个平台上线?是否已上线Binance和OKX 2025-06-30
-
ORDI首次发行时间与首发方式介绍 2025-06-30
-
加密货币基金热潮不歇!连续 11 周吸金、上周净流入 27 亿美元 2025-06-30
-
-
ORDI币上市时间及首日开盘价行情追踪 2025-06-30