jQuery遍历 is() 方法和 toggleClass()的使用
佀无极 2018-09-11 11:03:53 浏览94 评论0- 云栖社区
- 移动开发与客户端
- python
- 前端与交互设计
- javascript
- css
- html5
- js
- jquery
- html
- type
- list
摘要: 2017年12月27日 jQuery遍历 is() 方法和 toggleClass()的使用 <!DOCTYPE html> <html> <head> <script type="text/javascript" src="/jquery/jquery.
2017年12月27日
jQuery遍历 is() 方法和 toggleClass()的使用
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<style>
.active {background-color: #f00;}
.active-warn {background-color: #ff0;}
</style>
</head>
<body>
<ul>
<li><strong>list</strong> item 1 - one strong tag</li>
<li><strong>list</strong> item <strong>2</strong> -
two <span>strong tags</span></li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
<script>
$("li").click(function() {
var $li = $(this),
isWithTwo = $li.is(function() {
return $('strong', this).length === 2;
});
if ( isWithTwo ) {
$li.toggleClass("active-warn");
} else {
$li.toggleClass("active");
}
});
</script>
</body>
</html>
用云栖社区APP,舒服~
【云栖快讯】诚邀你用自己的技术能力来用心回答每一个问题,通过回答传承技术知识、经验、心得,问答专家期待你加入! 详情请点击- 分享到: