- js改变背景颜色本身并不难实现,
//改变id为teamIntroduction的dom元素的背景颜色为红色
var inputBackgroundColor=document.getElementById("teamIntroduction").style.backgroundColor='red';
- 但是这个过程中发现了一些问题,记录一下。
后来我把代码改成如下方式:
//先获取id=teamIntroduction的dom元素
var inputBackgroundColor=document.getElementById("teamIntroduction").style.backgroundColor;
//再改变背景颜色
inputBackgroundColor='red';
//代码执行结果:id=teamIntroduction的dom元素的背景颜色没有变为红色
- 之后我又加了测试语句,代码如下:
//先获取id=teamIntroduction的dom元素
var inputBackgroundColor=document.getElementById("teamIntroduction").style.backgroundColor;
//输出inputBackgroundColor值
alert(inputBackgroundColor);
//再改变变量inputBackgroundColor值
inputBackgroundColor='red';
//再次输出
alert(inputBackgroundColor);
/*输出结果:
*(空值)
* red
* 也就是说inputBackgroundColor的值确实是改变了,但是只是这个字符串的值改变了,
* id=teamIntroduction的dom元素的背景颜色并没有改变(如何写才能先获取,再改变有待后续思考)
*/