Home >
Web Front-end >
JS Tutorial >
JavaScript modifies css style style and dynamically changes element style_javascript skills
JavaScript modifies css style style and dynamically changes element style_javascript skills
WBOY
Release: 2016-05-16 17:08:51
Original
2628 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=”…” 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
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
The call is very simple, such as
Click me to change the style span> Newcomers 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 methods of calling 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:
Second: Define an internal style block object (Embedding a Style Block) You can insert a Block object. For definition methods, please refer to style sheet syntax. An example is as follows:
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.
The 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. An example is as follows:
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