JavaScript modifying CSS style styles changing element styles dynamically

Source: Internet
Author: User

  Modifying CSS style styles is more practical in some cases, you can change some styles dynamically, and then let's show you how to do it with JavaScript

One, the partial change style   divides into the change direct style, changes classname and changes csstext three kinds. Note that:  note that the case:  JavaScript is very sensitive to the case, ClassName can not write "n" written "n", Csstext can not write "T" written "T", otherwise can not achieve the effect.   Calling method:  if you change the ClassName, declare the class in the style sheet beforehand, but do not call style like document.getElementById (' obj '). Style.classname= "..." The wording is wrong! Can only be written: document.getElementById (' obj ') classname= "..."   change csstext  but if you use Csstext, you must add style, The correct writing is: document.getElementById (' obj '). style.csstext=     Change the direct style I don't have to say, you remember to write to the specific style, such as   code as follows: document.getElementById (' obj '). style.backgroundcolor= "#003366 ″    II, global change style   typically, We can change the value of the chain-style of href to achieve real-time page style switching, that is, "change the template style." This is the first time we need to give a target ID that needs to be changed, such as     <link rel = "stylesheet" type= "Text/css" id= "CSS" href= "Firefox.css"/>   Calls are simple, such as     <span on click= "javascript:document.getElementById (' CSS '). href = ' ie.css ' > Dot i change the style </span>  for newcomers often do not know how CSS specific styles are written in JavaScript, and sometimes require different browsers. such as float in IE written stylefloat, in Firefox written in Cssfloat, which requires the accumulation of everyone. Search for "Ccvita JavaScript" in Google, perhapswill help you with your doubts.     Basics     Typically there are three ways to call a style sheet in a Web page.   First: Link to an external style sheet file (linking to a style Sheet)   You can create an external style sheet file (. css) First, and then use the HTML link object. For example, the following:  copy code code as follows: <head>  <title> document title </title>  <link rel=stylesheet href= "http:// "type=" Text/css ">  </link></head>   " in XML, you should add in the declaration area as shown in the following example:   Copy code code as follows: <? Xml-stylesheet type= "Text/css" href= "Http://"?>    Second: Defining an internal style block object (embedding A Style block   You can insert a     block object between your HTML document and the tag. Define how to see style sheet syntax. Examples below:    code are as follows: <html>  <head>  <title> document title </title>  <style type= " Text/css ">  <!--  body {font:10pt" Arial "}  h1 {font:15pt/17pt" Arial "; font-weight:bold aroon}  h2 {font:13pt/15pt "Arial"; font-weight:bold; color:blue}  p {font:10pt/12pt "Arial"; color:black}& nbsp -->  </style>  </head>  <body>  </body></html>    Note that the Type property of the Style object is set to "Text/css" here, is to allow browsers that do not support this type to ignore style sheets.     Third: inline definition (Inline Styles)   Inline definition is to use the object's Style property to define the style sheet properties that apply to the object's markup. For example below:  code is as follows: <p style= "margin-left:0.5in; margin-right:0.5in "> This line has been added around the outer patch </p><p> </p>     
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.