您必须缩短前缀名称并删除连字符。使用 camel-case 除非它是前缀 - 然后第一个 char 也必须大写:
var ul= document.getElementById('multi-columns');
ul.style['MozColumnCount'] = 4;
ul.style['WebkitColumnCount'] = 4;
ul.style['columnCount'] = 4;
或者
ul.style.MozColumnCount = 4;
ul.style.WebkitColumnCount = 4;
ul.style.columnCount = 4;
您可以通过使用如下函数来概括这一点:
function getSupportedProp(proparray){
var root=document.documentElement //reference root element of document
for (var i=0; i<proparray.length; i++){ //loop through possible properties
if (proparray[i] in root.style){ //if property exists on element (value will be string, empty string if not set)
return proparray[i] //return that string
}
}
}
//SAMPLE USAGE
var boxshadowprop = getSupportedProp(['boxShadow', 'MozBoxShadow', 'WebkitBoxShadow']) //get appropriate CSS3 box-shadow property
document.getElementById("mydiv").style[boxshadowprop]="5px 5px 1px #818181" //set CSS shadow for "mydiv"