Several Methods for calling styles using scripts

Source: Internet
Author: User

Previously, I often met words such as classname and csstext, but I don't know how to use them. I recently started to build my website and gained a lot. I would like to share with others who do not understand it.

There are four methods to call a style sheet on a webpage. The first is the external chain, that is, the form of <link rel = "StyleSheet" href = "/control/css/base.css">, the second is the input style sheet, and the third is the declaration in the webpage header, for example,
I. In general, we can change the value of href of the external link style to implement real-time switching of the webpage style, that is, "changing the template style ". At this time, we first need to assign an id to the target to be changed, as shown in figure

<Link rel = "stylesheet" type = "text/css" id = "css" href = "firefox.css"/>

It is easy to call, for example, <span on click = "javascript: document. getElementById ('css '). href = 'ie.css'"> click I to change the style </span>

2. Partial style change: Direct style change, className change, and cssText change. Note: first, javascript is very case sensitive. className cannot write "N" as "n", and cssText cannot write "T" as "t ", otherwise, the effect cannot be achieved. Second, if you change the className, declare the class in the style sheet in advance.
Document. getElementById ('obj '). style. className = "..." is incorrectly written!
Can only be written as: document. getElementById ('obj '). className = "..."

However, if cssText is used, style must be added. The correct syntax is as follows:
Document.getElementById('obj').style.css Text = "..."

I don't have to talk about changing the direct style. You just need to write it into the specific style, as shown in
Document. getElementById ('obj '). style. backgroundColor = "#003366"
New users often do not know how to write 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 your accumulation. Searching for "JSS" in google may be helpful to your doubts.

Related Article

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.