jQuery-Selectors(選擇器)的使用(三、簡單篇)
系列文章導航:
jQuery-Selectors(選擇器)的使用(一、基本篇)
jQuery-Selectors(選擇器)的使用(二、層次篇)
jQuery-Selectors(選擇器)的使用(三、簡單篇)
jQuery-Selectors(選擇器)的使用(四--五、內容篇&可見性篇)
jQuery-Selectors(選擇器)的使用(六、屬性篇)
jQuery-Selectors(選擇器)的使用(七、子元素篇)
jQuery-Selectors(選擇器)的使用(八、表單篇)
jQuery-Selectors(選擇器)的使用(九、表單對象屬性篇)
1. :first用法
定 義:匹配找到的第一個元素
返回值:Element
實 例:將ID為"ul_1"的ul中的第一個Li元素的背景色改為紅色
代 碼: $("#ul_1 li:first").css("background-color","red"); //點擊按鈕一將執行這句代碼
定 義:匹配找到的第一個元素
返回值:Element
實 例:將ID為"ul_1"的ul中的第一個Li元素的背景色改為紅色
代 碼: $("#ul_1 li:first").css("background-color","red"); //點擊按鈕一將執行這句代碼
- ul ID="ul_1"
- Li 1
- Li 2
- Li 3
- Li 4
- Li 5
- Li 6
- Li 7
- Li 8
2. :last用法
定 義:匹配找到的最后一個元素
返回值:Element
實 例:將ID為"ul_2"的ul中的最后一個Li元素的背景色改為紅色
代 碼: $("#ul_2 li:last").css("background-color","red"); //點擊按鈕二將執行這句代碼
- ul ID="ul_2"
- Li 1
- Li 2
- Li 3
- Li 4
- Li 5
- Li 6
- Li 7
- Li 8
3. :not(selector)用法
定 義:去除所有與給定選擇器匹配的元素
返回值:Array
參 數:selector (Selector) : 用于篩選的選擇器
實 例:將ID為"ul_3"的ul中除最后一個Li元素以外的其他Li元素的背景色改為紅色
代 碼: $("#ul_3 li:not(li:last)").css("background-color","red"); //點擊按鈕三將執行這句代碼
- ul ID="ul_3"
- Li 1
- Li 2
- Li 3
- Li 4
- Li 5
- Li 6
- Li 7
- Li 8
擴展:你可以試試執行 $("li:not(li:last)").css("background-color","red"); 這句代碼,看看有什么效果
4. :even用法
定 義:匹配所有索引值為偶數的元素,從 0 開始計數
返回值:Array
實 例:將ID為"ul_4"的ul中索引為偶數的Li元素的背景色改為紅色(注:索引從0開始)
代 碼: $("#ul_4 li:even").css("background-color","red"); //點擊按鈕四將執行這句代碼
- ul ID="ul_4"
- Li 1
- Li 2
- Li 3
- Li 4
- Li 5
- Li 6
- Li 7
- Li 8
5. :odd用法
定 義:匹配所有索引值為奇數的元素,從 0 開始計數
返回值:Array
實 例:將ID為"ul_5"的ul中索引為奇數的Li元素的背景色改為紅色(注:索引從0開始)
代 碼: $("#ul_5 li:odd").css("background-color","red"); //點擊按鈕五將執行這句代碼
- ul ID="ul_5"
- Li 1
- Li 2
- Li 3
- Li 4
- Li 5
- Li 6
- Li 7
- Li 8
6. :eq(index)用法
定 義:匹配一個給定索引值的元素
返回值:Element
參 數:index (Number) : 從 0 開始計數
實 例:將ID為"ul_6"的ul中索引為3的Li元素的背景色改為紅色(注:索引從0開始)
代 碼: $("#ul_6 li:eq(3)").css("background-color","red"); //點擊按鈕六將執行這句代碼
- ul ID="ul_6"
- Li 1
- Li 2
- Li 3
- Li 4
- Li 5
- Li 6
- Li 7
- Li 8
7. :gt(index)用法
定 義:匹配所有大于給定索引值的元素
返回值:Array
參 數:index (Number) : 從 0 開始計數
實 例:將ID為"ul_7"的ul中索引值大于3的Li元素的背景色改為紅色(注:索引從0開始)
代 碼: $("#ul_7 li:gt(3)").css("background-color","red"); //點擊按鈕七將執行這句代碼
- ul ID="ul_7"
- Li 1
- Li 2
- Li 3
- Li 4
- Li 5
- Li 6
- Li 7
- Li 8
8. :lt(index)用法
定 義:匹配所有小于給定索引值的元素
返回值:Array
參 數:index (Number) : 從 0 開始計數
實 例:將ID為"ul_8"的ul中索引值小于3的Li元素的背景色改為紅色(注:索引從0開始)
代 碼: $("#ul_8 li:lt(3)").css("background-color","red"); //點擊按鈕八將執行這句代碼
- ul ID="ul_8"
- Li 1
- Li 2
- Li 3
- Li 4
- Li 5
- Li 6
- Li 7
- Li 8
9. :header用法
定 義:匹配如 h1, h2, h3之類的標題元素
返回值:Array
實 例:將ID為"div_1"的DIV中所有header(標題)元素的背景色改為紅色
代 碼: $("#div_1 :header").css("background-color","red"); //點擊按鈕九將執行這句代碼
DIV ID="div_1"
P標記
span標記H1
H2
H3
H4
H5
H6
10. :animated用法
定 義:匹配所有正在執行動畫效果的元素
返回值:Array
實 例:將ID為"div_2"的DIV中沒有執行動畫效果的元素的背景色改為紅色
代 碼: $("#div_2 :not(:animated)").css("background-color","red"); //點擊按鈕十將執行這句代碼
DIV id="div_2"
span標記
span標記
你可以下載這篇文章的HTML源文件:download
全站熱搜