Javascript controls CSS

Source: Internet
Author: User
I checked JS and CSS in the last row. Then I suddenly wanted to try JavaScript to control CSS. After all, it may be used in the future. I checked the following information on the Internet:

Assignment Method:Usage: element (node .style.css attributes
CSS attribute writing is special.
A word can be written directly, with the first letter in the middle of the horizontal bar in lower case, and the last letter in upper case, without horizontal bar links
There are several other special features, such as writing the float attribute of common CSS
Document. getelementbyid ("div01" ).style.css float;
Because float is a reserved JS word.
There is no other difference.
Document. getelementbyid ("div01"). style. height;
Document. getelementbyid ("div01"). style. lineheight;
Document. getelementbyid ("div01"). style. backgroundcolor;

The following is an in-depth example. You can customize the CSS attributes in an object and use these attributes to set the style of the current node:

Function changecss (){
VaR S = {backgroundcolor: "# 0099ff", width: "40px"}; // contains CSS attributes
VaR node = Document. getelementbyid ("A"); get the node
For (var c in S ){
EVAl ("node. style. "+ C +" = s [c] "); // The original execution is as follows: node. style. proname = s [c]; however, because proname (attribute name) is unknown and needs to be retrieved through S object, EVThrough this function, the stored string can be executed as an expression. This is a very practical method!
}
}

Ii. Use the classname attribute of a node to change its CSS

Another way is to control the class attribute of the tag to change the overall CSS style. Usage:

<Font id = "A"> AA </font>

//////////////////////////////////////// /////////

<Style id = "CSS" type = "text/CSS">

. CSS {

Background: red;

}

</Style>

//////////////////////////////////////// ////////////

VaR node = Document. getelementbyid ("");

Node. classname = "CSS ";

3. Get CSS attribute values related to nodes

The above method can be said to set a value to CSS or change CSS. How to get the CSS style attribute value of the current node? After a long time, I finally found out:

IE: node. currentstyle ['Property name']

Hh: Document. defaultview. getcomputedstyle (node, null) ['Property name'];

A comparison method on the internet is as follows:

Function getcurrentstyle (OBJ, Prop ){

If (obj. currentstyle ){

Return obj. currentstyle [prop];

}

Else if (window. getcomputedstyle ){

Propprop = prop. Replace (/([A-Z])/g, "-$1 ");

Propprop = prop. tolowercase ();

Return document. defaultview. getcomputedstyle (OBJ, null) [prop];

}

}

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.