jQuery教程06-基本筛选选择器
生活随笔
收集整理的這篇文章主要介紹了
jQuery教程06-基本筛选选择器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
jQuery選擇器之基本篩選選擇器
很多時候我們不能直接通過基本選擇器與層級選擇器找到我們想要的元素,為此jQuery提供了一系列的篩選選擇器用來更快捷的找到所需的DOM元素。篩選選擇器很多都不是CSS的規范,而是jQuery自己為了開發者的便利延展出來的選擇器
1、篩選選擇器
篩選選擇器的用法與CSS中的偽元素相似,選擇器用冒號“:”開頭,通過一個列表,看看基本篩選器的描述:
2、注意事項:
:eq(), :lt(), :gt(), :even, :odd 用來篩選他們前面的匹配表達式的集合元素,根據之前匹配的元素在進一步篩選,注意jQuery合集都是從0開始索引
gt是一個段落篩選,從指定索引的下一個開始,gt(1) 實際從2開始
3、示例代碼:
<!DOCTYPE html> <html> <head><meta http-equiv="Content-type" content="text/html; charset=utf-8" /><title>基本篩選選擇器</title><script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script><style>.left {width: auto;height: 120px;}.left div {width: 70px;height: 70px;padding: 5px;margin: 5px;float: left;background: #bbffaa;border: 1px solid #ccc;}.bottom {width: 800px;}.bottom div,.bottom span {display: block;width: 80px;height: 80px;margin: 5px;background: #bbffaa;float: left;font-size: 14px;}.bottom .small {width: 60px;height: 25px;font-size: 12px;background: #fab;}</style> </head> <body><h2>基本篩選器</h2><h3>:first/:last/:even/:odd</h3><div class="left"><div class="div"><p>div:first</p><p>:even</p></div><div class="div"><p>:odd</p></div><div class="div"><p>:even</p></div><div class="div"><p>:odd</p></div><div class="div"><p>:even</p></div><div class="div"><p>div:last</p><p>:odd</p></div></div><script type="text/javascript">//找到第一個div$('.div:first').css("color", "#CD00CD");</script><script type="text/javascript">//找到最后一個div$('.div:last').css("color", "#CD00CD");</script><script type="text/javascript">//:even 選擇所引值為偶數的元素,從 0 開始計數$('.div:even').css("border", "3px groove red");</script><script type="text/javascript">//:odd 選擇所引值為奇數的元素,從 0 開始計數$('.div:odd').css("border", "3px groove blue");</script><h3>:eq/:gt/:lt</h3><div class="left"><div class="aaron"><p>:lt(3) and :eq(0)</p></div><div class="aaron"><p>:lt(3) and :eq(1)</p></div><div class="aaron"><p>:lt(3) and :eq(2)</p></div><div class="aaron"><p>:eq(3)</p></div><div class="aaron"><p>:gt(3) and :eq(4)</p></div><div class="aaron"><p>:gt(3) and :eq(5)</p></div></div><script type="text/javascript">//:eq//選擇單個$('.aaron:eq(2)').css("border", "3px groove blue");</script><script type="text/javascript">//:gt 選擇匹配集合中所有索引值大于給定index參數的元素$('.aaron:eq(3)').css("border", "3px groove green");</script><script type="text/javascript">//:gt 選擇匹配集合中所有索引值大于給定index參數的元素$('.aaron:gt(3)').css("border", "3px groove red");</script><script type="text/javascript">//:lt 選擇匹配集合中所有索引值小于給定index參數的元素//與:gt相反$('.aaron:lt(3)').css("color", "red");</script><h3>:not</h3><div class="left"><div><input type="checkbox" name="a" /><p>Aaron</p></div><div><input type="checkbox" name="b" /><p>數據</p></div><div><input type="checkbox" name="c" checked="checked" /><p>其他</p></div></div><script type="text/javascript">//:not 選擇所有元素去除不匹配給定的選擇器的元素//選中所有緊接著沒有checked屬性的input元素后的p元素,賦予顏色$('input:not(:checked) + p').css("background-color", "#CD00CD");</script> </body></html>總結
以上是生活随笔為你收集整理的jQuery教程06-基本筛选选择器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MAC 设置$PATH 关闭termin
- 下一篇: hawq state 报错: the d