[ <li>Karl</li>, <li>Tane</li> ]
--------------------------------------------------------------------------------
:first-child
匹配第一個子元素
':first' 只匹配一個元素,而此選擇符將為每個父元素匹配一個子元素
Matches the first child of its parent.
While ':first' matches only a single element, this matches more then one: One for each parent.
返回值
Array<Element>
示例
在每個 ul 中查找第一個 li
HTML 代碼:
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul>
jQuery 代碼:
$("ul li:first-child")
結果:
[ <li>John</li>, <li>Glen</li> ]
--------------------------------------------------------------------------------
:last-child
匹配最后一個子元素
':last'只匹配一個元素,而此選擇符將為每個父元素匹配一個子元素
Matches the last child of its parent.
While ':last' matches only a single element, this matches more then one: One for each parent.
返回值
Array<Element>
示例
在每個 ul 中查找最后一個 li
HTML 代碼:
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul>
jQuery 代碼:
$("ul li:last-child")
結果:
[ <li>Brandon</li>, <li>Ralph</li> ]
--------------------------------------------------------------------------------
:only-child
如果某個元素是父元素中唯一的子元素,那將會被匹配
如果父元素中含有其他元素,那將不會被匹配。
Matches the only child of its parent.
If the parent has other child elements, nothing is matched.
返回值
Array<Element>
示例
在 ul 中查找是唯一子元素的 li
HTML 代碼:
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
jQuery 代碼:
$("ul li:only-child")
結果:
[ <li>Glen</li> ]
--------------------------------------------------------------------------------
:input
匹配所有 input, textarea, select 和 button 元素
Matches all input, textarea, select and button elements.
返回值
Array<Element>
示例
查找所有的input元素
HTML 代碼:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代碼:
$(":input")
結果:
[ <input type="text" />, <input type="checkbox" />, <input type="radio" />, <input type="image" />, <input type="file" />, <input type="submit" />, <input type="reset" />, <input type="password" />, <input type="button" /> ]
--------------------------------------------------------------------------------
:text
匹配所有的單行文本框
Matches all input elements of type text.
返回值
Array<Element>
示例
查找所有文本框
HTML 代碼:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代碼:
$(":text")
結果:
[ <input type="text" /> ]
--------------------------------------------------------------------------------
:password
匹配所有密碼框
Matches all input elements of type password.
返回值
Array<Element>
示例
查找所有密碼框
HTML 代碼:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代碼:
$(":password")
結果:
[ <input type="password" /> ]
--------------------------------------------------------------------------------
:radio
匹配所有單選按鈕
Matches all input elements of type radio.
返回值
Array<Element>
示例
查找所有單選按鈕
HTML 代碼:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代碼:
$(":radio")
結果:
[ <input type="radio" /> ]
--------------------------------------------------------------------------------
:submit
匹配所有提交按鈕
Matches all input elements of type submit.
返回值
Array<Element>
示例
查找所有提交按鈕
HTML 代碼:
<form>
<input type="text" />
<input type="checkbox" />
<input type="radio" />
<input type="image" />
<input type="file" />
<input type="submit" />
<input type="reset" />
<input type="password" />
<input type="button" />
<select><option/></select>
<textarea></textarea>
<button></button>
</form>
jQuery 代碼:
$(":submit")
結果:
[ <input type="submit" /> ]
其他的一些 都是一樣的道理:image ,:reset,:button,:file,:hidden !@#@!%$%