How to modify css styles by using JavaScript to dynamically change element styles _ javascript skills

Source: Internet
Author: User
It is more practical to modify css styles in some cases. You can dynamically change some styles. Next we will introduce how to use JavaScript. 1. Partially change styles.
You can change the direct style, className, and cssText. Note that:
Note case sensitivity:
Javascript is case sensitive. className cannot write "N" as "n", and cssText cannot write "T" as "t". Otherwise, the effect cannot be achieved.
Call method:
If you change the className, declare the class in the style sheet in advance, but do not declare the class with the style, such as document. getElementById ('obj '). style. className = "..." Incorrect syntax! Can only be written as: document. getElementById ('obj '). className = "..."
Change cssText
However, if cssText is used, style must be added. The correct method is document.getElementById('obj').style.css Text = "..."

I don't have to talk about changing the direct style. You just need to write it into the specific style, as shown in

The Code is as follows:


Document. getElementById ('obj '). style. backgroundColor = "#003366 ″


Ii. Global style change
In general, we can change the value of href of the external link style to implement real-time switching of the webpage style, that is, "changing the template style ". At this time, we first need to assign an id to the target to be changed, as shown in figure


It is easy to call, such

Click to change the style
New users often do not know how to write CSS styles in javascript, and sometimes the requirements are different in different browsers. For example, float is written as styleFloat in IE and cssFloat in FIREFOX, which requires your accumulation. Searching for "ccvita javascript" in google may be helpful to your doubts.

Basic knowledge

There are usually three methods to call a style sheet on a webpage.
First: link the external Style Sheet file (Linking to a Style Sheet)
You can create an external table file (.css) and then use the link object of HTML. Example:

The Code is as follows:



Document title



In XML, you should add it to the declaration area as follows:

The Code is as follows:


<? Xml-stylesheet type = "text/css" href = "http://www.dhtmlet.com/dhtmlet.css? 1.1.9 "?>


Type 2: Define an internal Style Block object (Embedding a Style Block)
You can insert

Block object. For the definition method, see style sheet syntax. Example:

The Code is as follows:




Document title





Note that the type attribute of the style object is set to "text/css", which allows browsers that do not support this type to ignore style forms.

Third: Inline Styles)
Inline definition is to use the style attribute Definition of the object within the object tag to apply its style table attribute. Example:

The Code is as follows:


This line has been added with external patches.

Related Article

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.