脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-jQuery中addClass()方法用法实例教程,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
本文实例讲述了jQuery中addClass()方法用法。分享给大家供大家参考。具体分析如下:
此方法向匹配元素添加一个或多个类。
此方法有多个语法形式。
语法结构一:
为匹配元素添加指定的类名。如果要一次性添加多个类名,它们之间要用空格分隔。
代码如下:
$(selector).addClass(class)
参数列表:
实例代码:
代码如下:
<!DOCTYPE htML>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="https://www.cnblogs.COM/" />
<tITle>addClass函数-博客园</title>
<style type="text/css">
p{
height:200px;
width:200px;
}
.border{
border:1px solid red;
}
.reset{
font-Size:20px;
color:green;
}
</style>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#BTn").click(function(){
$("p").addClass("border reset");
})
})
</script>
</head>
<body>
<p>博客园欢迎您</p>
<button id="btn">点击查看效果</button>
</body>
</html>
以上代码可以为p添加两个类,能够设置p的边框和字体的样式。
语法结构二:
以函数的返回值作为要添加的类名。
代码如下:
$(selector).addClass(function(index,oldclass))
参数列表:
实例代码:
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="https://www.cnbLOGs.com/" />
<title>addclass()函数-博客园</title>
<style type="text/css">
p{
height:200px;
width:200px;
}
.border{
border:1px solid red;
}
.reset{
font-size:20px;
color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$("p").addClass(function(){
return "border reset";
})
})
})
</script>
</head>
<body>
<p>博客园欢迎您</p>
<button id="btn">点击查看效果</button>
</body>
</html>
上面代码和第一个实例的功能是一样的,只不过要添加的类是通过函数返回值得到的。
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-jQuery中addClass()方法用法实例教程全部内容,希望文章能够帮你解决javascript代码实例教程-jQuery中addClass()方法用法实例教程所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。