Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JavaScript local modification and global modification of css style examples

伊谢尔伦
Release: 2017-07-21 14:59:34
Original
2105 people have browsed it

1. Partially changing the style

is divided into three types: changing the direct style, changing the className and changing the cssText. What needs to be noted is:
Pay attention to capitalization:
Javascript is very sensitive to capitalization. ClassName cannot write "N" as "n", and cssText cannot write "T" as "t", otherwise it cannot be implemented. Effect.
Calling method:
If you change className, declare the class in the style sheet in advance, but do not follow style when calling, like document.getElementById('obj').style.className=”…” Incorrect! 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 the specific style, such as

document.getElementById('obj').style.backgroundColor=”#003366″
Copy after login


2. Global Changing 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" />
Copy after login

It is very simple to call, such as

<span on click="javascript:document.getElementById(&#39;css&#39;).href = &#39;ie.css&#39;">点我改变样式</span>
Copy after login

For newcomers, they often don’t know that the specific CSS style is in javascript How to write it, 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.
First type: Linking 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. An example is as follows:

<head> 
<title>文档标题</title> 
<link rel=stylesheet href="http://www.ccvita.com/demo.css" type="text/css"> 
</link></head>
Copy after login

In XML, you should add it in the declaration area as shown in the following example:

< ? xml-stylesheet type="text/css" href="http://www.dhtmlet.com/dhtmlet.css" ?>
Copy after login

Second: Define internal style block objects ( Embedding a Style Block)
You can insert a block object between the and tags of your HTML document. For definition methods, please refer to style sheet syntax. An example is as follows:

<html> 
<head> 
<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>
Copy after login

Please note that setting the type attribute of the style object to "text/css" here allows browsers that do not support this type to ignore the style sheet.

Third type: Inline Definition (Inline Styles)

Inline definition is to use the style attribute of the object within the mark of the object to define the style sheet attributes that apply to it. An example is as follows:

<p style="margin-left: 0.5in; margin-right:0.5in">这一行被增加了左右的外补丁</p><p> </p>
Copy after login

The above is the detailed content of Detailed explanation of JavaScript local modification and global modification of css style examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template