Home > Web Front-end > Front-end Q&A > How to reference css in JavaScript

How to reference css in JavaScript

WBOY
Release: 2023-05-29 16:46:37
Original
2167 people have browsed it

JavaScript is a programming language widely used in Web development. It can be used to add interactive effects and dynamic functions to web pages. In web development, CSS is also a crucial part, used to define the style and layout of web pages. So, how do you reference CSS in JavaScript? This article will introduce it in detail.

In HTML, use the tag to reference CSS files, as shown below:

<head>
  <link rel="stylesheet" href="style.css">
</head>
Copy after login

In JavaScript, we can reference CSS files through the following methods:

1. Use DOM operations to introduce CSS files

DOM (Document Object Model) is a programming interface that parses HTML documents into objects. Using DOM operations allows us to dynamically create, delete, and modify HTML elements in JavaScript. Using DOM operations in JavaScript can also implement the function of introducing CSS files. The specific method is as follows:

function loadCSS(url) {
  var link = document.createElement("link");
  link.type = "text/css";
  link.rel = "stylesheet";
  link.href = url;
  document.getElementsByTagName("head")[0].appendChild(link);
}
loadCSS("style.css");
Copy after login

The loadCSS function in the above code will dynamically create a tag and set the href attribute to point to the CSS file. path and add it to the tag.

2. Use JavaScript to change the class attribute of HTML elements

In HTML, we can use the class attribute to add styles to elements. In CSS style sheets, we can select the specified class through the selector. Add styles to elements. In JavaScript, we can change the style of an HTML element by changing its class attribute. Therefore, we can take advantage of this to dynamically reference CSS files.

function loadCSS(url) {
  var link = document.createElement("link");
  link.type = "text/css";
  link.rel = "stylesheet";
  link.href = url;
  document.getElementsByTagName("head")[0].appendChild(link);
}
loadCSS("style.css");

// 获取需要添加样式的元素
var element = document.getElementById("example");

// 改变元素的class属性
element.className = "example-class";
Copy after login

In the above code, we use the loadCSS function to introduce the CSS file, then obtain the element that needs to be styled through the getElementById method, and then modify the class attribute of the element. In the CSS style sheet, you can add styles to this element based on the example-class selector.

3. Use JavaScript to change CSS styles

In addition to changing the class attribute of HTML elements, we can also directly use JavaScript to modify CSS styles. The specific method is as follows:

function loadCSS(url) {
  var link = document.createElement("link");
  link.type = "text/css";
  link.rel = "stylesheet";
  link.href = url;
  document.getElementsByTagName("head")[0].appendChild(link);
}
loadCSS("style.css");

// 获取需要添加样式的元素
var element = document.getElementById("example");

// 修改元素的CSS样式
element.style.color = "red";
element.style.backgroundColor = "green";
Copy after login

In the above code, we also introduce the CSS file first, then use the getElementById method to obtain the element that needs to be styled, and then directly use element.style to modify the CSS style of the element.

Summary

Referencing CSS files in JavaScript allows us to add styles more flexibly in web page production. This article introduces three ways to reference CSS files, including using DOM manipulation references, changing the class attribute of HTML elements, and directly modifying CSS styles. For different usage scenarios, we can choose different ways to implement the function of introducing CSS files.

The above is the detailed content of How to reference css in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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