jq选择器

一 概念


1、css语法匹配

```
标签 | 类 | id | 交集
群组 | 后代 | 兄弟
伪类 | 属性
```

2、索引匹配

```
:eq(index) | :gt(index) | :lt(index)

二 代码示范

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>jq选择器</title>
<style type="text/css">
ul {
margin: 0;
padding: 0;
list-style: none;
width: 150px;
background-color: orange;
}
ul:after {
content: "";
display: block;
clear: both;
}
li {
width: 50px;
height: 50px;
background-color: red;
border-radius: 50%;
float: left;
}
</style>
</head>
<body>
<ul class="ul">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
<script src="js/jquery-3.3.1.js"></script>
<script type="text/javascript">
// console.log($);

// 获取页面所有的li
// 1.css语法匹配
// var $lis = $('li');
// var $lis = $('ul li');
var $lis = $('.ul>li');
console.log($lis);

// 整体操作
// i) 操作样式
$lis.css({
'background-color': 'cyan'
})
// ii) 事件
$lis.on('click', function () {
var index = $(this).index();
console.log(index);
var width = $(this).width();
console.log(width);
})


// 获取最中间一列
$('li:nth-child(3n-1)').on('click', function () {
console.log("中间列");
})
// 可以绑定多个事件

// 最后一排
// 2.索引匹配
$('li:gt(5)').on('click', function () {
console.log("最后一4排");
})

// 对角线
// $('li:even').on('click', function () {
// console.log("对角线");
// })

$('li:not(:nth-child(2n))').on('click', function () {
console.log("对角线");
})

</script>
</html>

posted @ 2018-10-23 16:13  不沉之月  阅读(75)  评论(0)    收藏  举报