CodeLab
Show Output
 
xxxxxxxxxx
37
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>Selecting Elements that Didn't Pass a Function Test in jQuery</title>
6
<style>
7
    .highlight{
8
        background: yellow;
9
    }        
10
</style>
11
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
12
<script>
13
$(document).ready(function(){
14
    $("ul li").not(function(index){
15
        return index % 2 !== 0;
16
    }).addClass("highlight");
17
});
18
</script>
19
</head>
20
<body>
21
    <h2>Unordered List</h2>
22
    <ul>
23
        <li>First list item</li>
24
        <li>Second list item</li>
25
        <li>Third list item</li>
26
        <li>Fourth list item</li>
27
    </ul>
28
    <hr>
29
    <h2>Another Unordered List</h2>
30
    <ul>
31
        <li>First list item</li>
32
        <li>Second list item</li>
33
        <li>Third list item</li>
34
        <li>Fourth list item</li>
35
    </ul>
36
</body>
37
</html>
 

在微博、微信、公众号、小程序分享此示例。 如果您觉得有帮助,请给我们一个赞。