我們來看看所有的jQuery選擇器。
正如您在上一課中看到的,jQuery選擇器以美元符號和圓括號開頭:$()。
最基本的選擇器是元素選擇器,它根據(jù)元素名稱選擇所有元素。
$("div") // selects all <div> elements
接下來是id和類選擇器,它們通過id和類名來選擇元素:
$("#test") // select the element with the id="test" $(".menu") //selects all elements with class="menu"
您還可以對選擇器使用以下語法:
$("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更容易訪問HTML DOM元素。
("P ")
$("div > ")