js改变背景颜色有感

  • 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元素的背景颜色并没有改变(如何写才能先获取,再改变有待后续思考)       
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值