本文介绍了如何在div标签上制作背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此代码:
<div data-role="page" id="two">
<div data-role="content" >
<div id="pic1"></div>
</div>
和此javascript代码:
and this javascript code:
<script type="text/javascript">
document.getElementById("pic1").setAttribute("background-color","red");
</script>
问题是我的div没有获得红色作为背景!怎么了?
The problem is that my div doesn't get the red color as a background!What's wrong?
推荐答案
您正在寻找元素上的 style
属性,没有 background-color
属性:
document.getElementById("pic1").style.backgroundColor = "red";
也就是说,通常最好使用CSS设置样式并添加/删除类.
That said, generally best to style things with CSS and add/remove classes.
这篇关于如何在div标签上制作背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!