- 本次练习封装设置CSS样式的方法,通过标签属性ele,属性名attr,属性值value直接设置CSS的样式,设置css样式的方法主要通过ele.style.attr的方式实现
function setCss(ele, attr, value) {
if (attr === 'opacity') {
ele.style.opacity = value;
ele.style.filter = "alpha(opacity = "+ value * 100+")";
return ;
}
var reg = /^(width|height|(padding|margin)?(left|right|top|bottom)?)$/i;
//value 进行单位处理
if (reg.test(attr)) {
if (!isNaN(value)) {
value += 'px';
}
}
ele.style[attr] = value;
}