jQueryのフィルタセレクタ
3735 ワード
<span style="color: #000000;">
$(</span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){
$(</span>"ul li:first").css("color","yellow"<span style="color: #000000;">);
$(</span>"ul li:last").css("color","pink"<span style="color: #000000;">);
$(</span>"ul li:odd").css("color","blue"<span style="color: #000000;">);
})
</span><span style="color: #008000;">//</span><span style="color: #008000;"> :first </span>
<span style="color: #008000;">//</span><span style="color: #008000;"> :last </span>
<span style="color: #008000;">//</span><span style="color: #008000;"> :eq(index) </span>
<span style="color: #008000;">//</span><span style="color: #008000;"> :odd </span>
<span style="color: #008000;">//</span><span style="color: #008000;"> :even </span>
<span style="color: #008000;">//</span><span style="color: #008000;"> jquery ul li</span>
console.log($("ul li:first").css("color","blue"<span style="color: #000000;">));
$(</span>"ul li").eq(0<span style="color: #000000;">);
$(</span>"ul li"<span style="color: #000000;">).first();
$(</span>"ul li").slice(0,1);<span style="color: #008000;">//</span><span style="color: #008000;">slice , </span>
<span style="color: #008000;">//</span><span style="color: #008000;"> </span>
$(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
</span><span style="color: #0000ff;">var</span> list=$("ul"<span style="color: #000000;">);
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> i = 0; i < list.length; i++<span style="color: #000000;">) {
$(</span>"ul:eq("+i+") li:first").css("background","red"<span style="color: #000000;">);
}
});
</span><span style="color: #008000;">//</span><span style="color: #008000;"> </span>
$(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
$(</span>"ul").each(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
$(</span><span style="color: #0000ff;">this</span>).children().first().css("background","red"<span style="color: #000000;">);
});
});
</span><span style="color: #008000;">//</span><span style="color: #008000;"> </span>
$(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
$(</span>"ul li:nth-child(1)").css("background","red"<span style="color: #000000;">);
});
</span><span style="color: #008000;">//</span><span style="color: #008000;"> </span>
$(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
$(</span>"ul li:first-child").css("background","red"<span style="color: #000000;">);
});
</span>