Today the time to toss a mask layer, encountered a problem, is to set style, look at the following:
Generally we want to set a style that is less like this:
Element.style.height = "50px"; element.style.width = "200px";
Ok, usually the style is less so use no problem, but if you encounter more to set up, still do? Obviously not, that's what I did:
Element.style = "Display:block;position:fixed;top:0;left:0;width:100%;height:100%;background: #F00";
I admit that I did not test, immediately write the mask to share to friends, the result was friends remind style can not directly assign value, I test a bit, in Firefox open gate no problem, but in Google IE assignment is not successful, hurriedly find the next information,
The original Element.style is a read-only property and cannot be assigned directly (but can assign values to sub-attributes, such as Element.style.width).
So what? Here are two ways to do this:
1. csstext attribute must return a serialized CSS rule
We can use that.
Element.style.cssText = "Display:block;position:fixed;top:0;left:0;width:100%;height:100%;background: #F00";
(Note: If the element style property already has other values, the Csstext setting will first clear the original and then assign the value)
2. For in
var function (element,styles) { forvar in styles) { = styles[i]; } }
Use:
SetStyle (element,{width: ' 200px '; position: ' absolute '; Left: ' 100px ';})
Finally, mask.
varOmask =NULL; varMask =function(backgroundcolor,transparency,zindex) {/**backgroundcolor for background color (default #000) Transparency for transparency (default 0.5 optional 0.1-0.9) ZIndex level (default 9999) **/ var_backgroundcolor = BackgroundColor? (BackgroundColor > 0?) "#000": backgroundcolor): "#000"; var_transparency = transparency? (Transparency > 0 && Transparency < 1 transparency: (BackgroundColor > 0? backgroundcolor:0.5)): (BackgroundColor> 0 && backgroundcolor < 1? backgroundcolor:0.5 ); var_zindex = ZIndex? (BackgroundColor >= 1? backgroundcolor:transparency): 9999; varSetStyle =function() {(Omask|| CREATEDIV). Style.csstext = "Display:block;position:fixed;top:0;left:0;width:100%;height:100%;background:" +_ Backgroundcolor+ "; Opacity:" +_transparency+ "; Filter:alpha (opacity=" +_transparency*100+ "); Z-index:" +_zindex+ ";"; } if(Omask) {SetStyle ();returnOmask;} varCreatediv = document.createelement ("div"); SetStyle (); Document.body.appendChild (Omask=creatediv); returnOmask; }
[JavaScript experience]element.style?]