1. Partially change the style
There are three types: changing direct style, changing className and changing cssText. Things to note are:
Pay attention to capitalization:
JavaScript is very case-sensitive. ClassName cannot write "N" as "n", and cssText cannot write "T" as "t", otherwise the effect cannot be achieved.
Calling method:
If you change className, declare the class in the style sheet in advance, but do not follow style when calling. Writing like document.getElementById('obj').style.className="..." is wrong! It can only be written as: document.getElementById('obj').className="…"
Change cssText
But if you use cssText, you must add style. The correct way to write it is: document.getElementById('obj').style.cssText=”…”
I don’t need to talk about changing the direct style. Just remember to write down the specific style, such as
Copy the code code as follows:
document.getElementById('obj').style.backgroundColor=”#003366″
2. Globally change the style
Normally, we can achieve real-time switching of web page styles by changing the href value of the external link style, that is, "changing the template style." At this time we first need to give an id to the target that needs to be changed, such as
<link rel = "stylesheet" type="text/css" id="css" href="firefox.css" />
It is very simple to call, such as
<span on click="javascript:document.getElementById('css').href = 'ie.css'">Click me to change the style</span>
Newbies often don’t know how to write specific 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 everyone's accumulation. Searching "ccvita javascript" in Google may help your doubts.
basic knowledge
There are usually three ways to call style sheets in web pages.
The first one: Linking to an external style sheet file (Linking to a Style Sheet)
You can first create an external style sheet file (.css) and then use the HTML link object. Examples are as follows:
Copy the code code as follows:
<head>
<title>Document title</title>
<link rel=stylesheet href="http://www.ccvita.com/demo.css" type="text/css">
</link></head>
In XML, you would add it in the declaration area as shown in the following example:
Copy the code code as follows:
< ?xml-stylesheet type="text/css" href="http://www.dhtmlet.com/dhtmlet.css" ?>
Second type: Define internal style block object (Embedding a Style Block)
You can insert a
block object. For definition methods, please refer to style sheet syntax. Examples are as follows:
Copy the code code 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></html>
Please note that setting the type attribute of the style object to "text/css" allows browsers that do not support this type to ignore the style sheet.
The third type: Inline definition (Inline Styles)
Inline definition is to use the object's style attribute within the object's markup to define the style sheet attributes that apply to it. Examples are as follows:
Copy the code code as follows:
<p style="margin-left: 0.5in; margin-right:0.5in">This line has added left and right outer patches</p><p> </p>