[JavaScript experience]element.style?]

Source: Internet
Author: User

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?]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.