您现在的位置: 万盛学电脑网 >> 程序编程 >> 数据库 >> 数据库综合 >> 正文

怎样设置或取得元素的CSS class

作者:佚名    责任编辑:admin    更新时间:2022-06-22

这篇文章主要介绍怎样设置或取得元素的CSS class的相关内容,下面我们就与大家一起分享。

jQuery支持方法用来操作HTML元素的CSS 属性

下面的方法为jQuery 提供的CSS操作方法:

addClass() – 为指定的元素添加一个或多个CSS类。

removeClass() – 为指定的元素删除一个或多个CSS类。

toggleClass() – 为指定的元素在添加/删除CSS类之间切换。

css() -设置或是取得CSS类型属性。

下面的StyleSheet为后面例子使用的CSS风格:

[css]

.important

{

font-weight:bold;

font-size:xx-large;

}

.blue

{

color:blue;

}

.important

{

font-weight:bold;

font-size:xx-large;

}

.blue

{

color:blue;

}

jQuery addClass示例

下面的例子为给定的元素添加CSS风格类

[html]

Heading 1Heading 2

 This is a paragraph.

 

This is another paragraph.

 

This is some important text!

 Heading 2

 This is a paragraph.

 

This is another paragraph.

 

This is some important text!

 

你也可以在addClass 添加多个类的名称,如:

[javascript]

$("button").click(function(){

$("#div1").addClass("important blue");

});

$("button").click(function(){

$("#div1").addClass("important blue");

});

jQuery removeClass 示例

[javascript]

$("button").click(function(){

$("h1,h2,p").removeClass("blue");

});

$("button").click(function(){

$("h1,h2,p").removeClass("blue");

});

jQuery toggle()示例,下面的例子使用toggle为HTML元素在添加/删除CSS类blue之间切换

[javascript]

$("button").click(function(){

$("h1,h2,p").toggleClass("blue");

});

以上就是我们为大家准备的怎样设置或取得元素的CSS class的相关内容,希望对大家可以有所帮助。