JavaScript modifies css styles and dynamically changes element styles.

Source: Internet
Author: User

JavaScript modifies css styles and dynamically changes element styles.

It is more practical to modify css styles in some cases. You can dynamically change some styles. Next we will introduce how JavaScript is used.

I. Partial style change

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

 

<Link rel = "stylesheet" type = "text/css" id = "css" href = "firefox.css"/>

It is easy to call, such

 

<Span on click = "javascript: document. getElementById ('css '). href = 'ie.css'"> click here to change the style </span>

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:

Copy the Code as follows:

<Head>

<Title> document title </title>

<Link rel = stylesheet href = "http://www.ccvita.com/demo.css" type = "text/css">

</Link>

 

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

Copy the Code as follows:

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

 

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:

<Html>

<Head>

<Title> document title </title>

<Style type = "text/css">

<! --

Body {font: 10pt "Arial "}

H1 {font: 15pt/17pt "Arial"; font-weight: bold; color: maroon}

H2 {font: 13pt/15pt "Arial"; font-weight: bold; color: blue}

P {font: 10pt/12pt "Arial"; color: black}

-->

</Style>

</Head>

<Body>

</Body>

 

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:

<P style = "margin-left: 0.5in; margin-right: 0.5in"> the left and right outer patches are added to this line. </p>

 

 

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.