jquery為element設(shè)置多屬性很簡(jiǎn)單瓦胎,見下:
$('#bigbox').css({
'background':'#666',
'width':'400px',
'height':'100px'
})
js默認(rèn)的方式芬萍,逐條添加,遇到需要添加多次屬性的情況搔啊,費(fèi)事柬祠,好處是兼容沒問題,見下:
var bigBox = document.getElementById('bigbox');
bigBox.style.backgroundColor = 'red';
bigBox.style.width = '400px';
bigBox.style.height = '100px';
想要修改多重屬性负芋,也可以使用setAttribute漫蛔,存在的缺陷是:會(huì)覆蓋掉默認(rèn)的或已經(jīng)修改過的屬性,見下:
var bigBox = document.getElementById('bigbox');
bigBox.setAttribute('style' , 'width:400px; height:300px; background-color:blue')
更為推薦的方法是使用cssText旧蛾,見下:
var bigBox = document.getElementById('bigbox');
bigBox.style.fontSize = '20px';
bigBox.style.cssText += 'color:#fff; background-color: #f96e5b; height: 100px;';
參考:
MDN-Style
MDN-cssText
ChangeSingle or Multiple Css Properties