我們來(lái)看看所有的jQuery選擇器。
正如您在上一課中看到的,jQuery選擇器以美元符號(hào)和圓括號(hào)開頭:$()。
最基本的選擇器是元素選擇器,它根據(jù)元素名稱選擇所有元素。
$("div") // selects all <div> elements
接下來(lái)是id和類選擇器,它們通過(guò)id和類名來(lái)選擇元素:
$("#test") // select the element with the id="test" $(".menu") //selects all elements with class="menu"
您還可以對(duì)選擇器使用以下語(yǔ)法:
$("div.menu") // all <div> elements with class="menu" $("p:first") // the first <p> element $("h1, p") // all <h1> and all <p> elements $("div p") // all <p> elements that are descendants of a <div> element $("*") // all elements of the DOM
選擇器比純JavaScript更容易訪問(wèn)HTML DOM元素。
("P ")
$("div > ")