js background的几种书写形式

<body>
        <div id="box" style="width: 300px; height: 300px; background-color: green;"> 
            <p id="text" style="background: mistyrose; width: 100px;">hello world!</p>     
            <img src="res/apple.png" height="300px" width="400px">    
        </div>
        
        <script type="text/javascript">
            let oDiv=document.getElementById("box")
            let oTxt=document.getElementById("text")
            let oImg=document.getElementsByTagName('img')[0]
            
            oDiv.style.background='yellow'
            oDiv.style.backgroundColor='palegoldenrod'
            
            //oDiv.style[] 用中括号可直接调用原生css 这里没有点.!!!! 
            oDiv.style['backgroundColor']='plum'
            oDiv.style['background-color']='yellow'
            //需要把backgroundColor写成驼峰式background-color 和上面对应 才能使用
            
            oDiv.style.setProperty('background','blue')
            // oDiv.style.setProperty('backgroundColor','teal')  
            oDiv.style.setProperty('background-color','yellowgreen')
            // .setProperty() 设置样式值 括号里(属性名,属性值)
            
            alert(oImg.getAttribute('width')) 
            //getAttribute()获取属性或字段 只能获取行内属性值 专门用来获取行内属性值
            
            oImg.setAttribute('width','100px')
            //setAttribute()设置属性 括号里(属性名,属性值)
            
        </script>
        
    </body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值