


Detailed explanation of JavaScript modification of css local and global style code
Modifying css style style is quite practical in some cases. You can dynamically change some styles. Next, I will introduce how to do it using JavaScript
1. Partially change the style
There are three types: changing direct style, changing className and changing 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. Remember to write the specific style, such as
document.getElementById('obj').style.backgroundColor=”#003366″
2. Globally change the style
Usually In this case, 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'">点我改变样式</span>
. Newcomers often do not 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 method: 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://demo.css" type="text/css"> </link></head>
In XML, you should add it in the declaration area as shown in the following example:
< ? xml-stylesheet type="text/css" href="http://dhtmlet.css" ?>
Second: Define an internal style block object (Embedding a Style Block)
You can insert a
block object between the and tags in 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>
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 object's style attribute within the object's tag to define the style sheet attributes that apply to it. Examples are as follows:
<p style="margin-left: 0.5in; margin-right:0.5in">这一行被增加了左右的外补丁</p><p> </p>
The above is the detailed content of Detailed explanation of JavaScript modification of css local and global style code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
