jquery中增加class的方法:1、利用“$(指定元素)”语句获取指定元素对象;2、利用addClass()方法给指定元素添加class,语法为“元素对象.addClass("新的class");”。

本教程操作环境:windows10系统、jquery3.2.1版本、Dell G3电脑。
jquery中怎样增加class
jquery提供了一个addClass()添加class类的方法,可以通过这个方法对标签进行class类添加。通过点击按钮添lass类使字体变大进行addClass()方法的演示。
addClass() 方法向被选元素添加一个或多个类。
该方法不会移除已存在的 class 属性,仅仅添加一个或多个 class 属性。
示例如下:
jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。
<script></script><script>
$(document).ready(function(){
$("button").click(function(){
$("p:first").addClass("intro");
});
});
</script><style>
.intro{
font-size: 40px;
}
</style><h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>向第一个 p 元素添加一个类</button>
输出结果:

相关视频教程推荐:jQuery视频教程










