我们来看看所有的jQuery选择器。
正如您在上一课中看到的,jQuery选择器以美元符号和圆括号开头:$()。
最基本的选择器是元素选择器,它根据元素名称选择所有元素。
1
$("div") // selects all <div> elements
$("div") // selects all <
div
> elements
接下来是id和类选择器,它们通过id和类名来选择元素:
2
$("#test") // select the element with the id="test"
$(".menu") //selects all elements with class="menu"
您还可以对选择器使用以下语法:
3
4
5
$("div.menu") // all <div> elements with class="menu"
$("div.menu") // all <
> elements with class="menu"
$("p:first") // the first <p> element
$("p:first") // the first <
p
> element
$("h1, p") // all <h1> and all <p> elements
$("h1, p") // all <
h1
> and all <
$("div p") // all <p> elements that are descendants of a <div> element
$("div p") // all <
> elements that are descendants of a <
$("*") // all elements of the DOM
选择器比纯JavaScript更容易访问HTML DOM元素。
("P ")
$("div > ")