jquery sub-element filter selector

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <link rel="stylesheet" href="imooc.css" type="text/css">
    <script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>

<body>
    <h2>子元素筛选选择器</h2>
    <h3>:first-child、:last-child、:only-child</h3>
    <div class="left first-div">
        <div class="div">
            <a>:first-child</a>
            <a>第二个元素</a>
            <a>:last-child</a>
        </div>
        <div class="div">
            <a>:first-child</a>
        </div>
        <div class="div">
            <a>:first-child</a>
            <a>第二个元素</a>
            <a>:last-child</a>
        </div>
    </div>

    < Script of the type = "text / JavaScript" > 
        // Find a class = first element under "First-div" 
        // For the first in all of its parent 
        $ ( ' .first a div-: First-Child ' ) .css ( " Color " , " # CD00CD " );
     </ Script >

    < Script of the type = "text / JavaScript" > 
        // Find a class = The last element under the "First-div" 
        // for last in all parent 
        // then, last is the first element if only one element 
        $ ( ' .first div-A: Last-Child ' ) .css ( " Color " , " Red " );
     </ Script >

    < Script type = "text / JavaScript" > 
        // Find class = only one child element under the element of a "First-div" 
        $ ( ' .first a div-: Child-only ' ) .css ( " Color " , " Blue " );
     </ Script >


    <h3>:nth-child、:nth-last-child</h3>
    <div class="left last-div">
        <div class="div">
            <a>:first-child</a>
            <a>第二个元素</a>
            <a>第三个元素</a>
            <a>:last-child</a>
        </div>
        <div class="div">
            < A > : First-Child </ A > 
            < A > The second element </ A > 
        </ div > 
        < div class = "div" > 
            < A > : First-Child </ A > 
            < A > second elements </ A > 
            < A > The third element </ A > 
            < A > : Last Child- </ A > 
        </ div > 
    </div>

    < Script type = "text / JavaScript" > 
        // Find class = second element in a "Last-div" 
        $ ( ' .last a div-: Child-Nth (2) ' ) .css ( " Color " , " # CD00CD " );
     </ Script >

    < Script type = "text / JavaScript" > 
        // Find class = penultimate element in a "Last-div" 
        $ ( ' .last a div-: Nth-Last-Child (2) ' ) .css ( " Color " , " Red " );
     </ Script >

</body>

</html>

 

Guess you like

Origin www.cnblogs.com/wzndkj/p/11731716.html
Recommended