Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

jQuery addClass()

addclass()方法用于向所选元素添加一个或多个类名。此方法仅用于在类属性中添加一个或多个类名称,而不会删除现有的类属性。
如果要添加多个类,请使用空格将类名称分隔。
语法:
$(selector).addClass(classname,function(index,oldclass))

jQuery addClass()方法的参数

参数 说明
classname 这是必填参数。它指定您要添加的一个或多个类名。
function(index,oldclass) 这是一个可选参数。它指定一个返回一个或多个要添加的类名称的函数。 索引:用于提供元素在集合中的索引位置。 Currentclass:用于返回所选元素的当前类名称。

jQuery addClass()方法的示例

我们以一个示例来演示jQuery addclass()方法的效果:
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p:first").addClass("intro");
    });
});
</script>
<style>
.intro {
    font-size: 200%;
    color: red;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Add a class name to the first p element</button>
</body>
</html>
输出:

This is a heading

This is a paragraph.

This is another paragraph.

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4