Home > Web Front-end > JS Tutorial > How to add css style to js

How to add css style to js

藏色散人
Release: 2023-01-06 11:13:36
Original
12477 people have browsed it

How to add css style in js: 1. Add style through "$("span").css("css attribute name", "attribute value")"; 2. Through "addClass()" Method to add css styles.

How to add css style to js

The operating environment of this article: windows7 system, jquery3.2.1&&css3 version, Dell G3 computer.

js adds css styles:

Since jquery supports css3, it is well compatible with many browsers, so it is better to use css styles through jquery.

  • js add css style method

If the css style is undefined, you can call the element's css() method to add the style.

$("span").css("css属性名","属性值")
Copy after login

Example: Set the fonts labeled span to red

$("span").css("color","red")
Copy after login

If it is a defined css style, it can be added through the addClass() method, such as

<style type="text/css">
   .aa {
        border:1px solid red;
    }
</style>
  
$("#txtName").addClass("aa");
<input id="txtName"type="text"value="请输入你的姓名"/>
Copy after login
  • How to operate css styles:

1、.css("样式"):获得样式值,比如$("input").css("color") 获得input中字体的颜色
2、.css("样式","value"):为样式赋值,如$("input").css("color","red");
3、.addClass("样式类1,样式类2,样式类3"):可以添加多个定义好的样式类
4、.hasClass("样式类类"):判断是否存在该样式
5、.toggleClass("样式类"):如果存在(不存在)就切换(删除)样式
6、.toggleClass("样式类",swith):如果swith为false,则删除样式,如果swith为true,则切换成该类
7、removeClass("样式类"):移除样式类
8、.css({样式名:"value",样式名:"value",样式名:"value"}):可以多次添加样式
Copy after login

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to add css style to js. 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