jQuery选择器有什么区别?
这是我以前见过的选择器列表:
$('.menu')
-
$('menu')
-
$('#menu')
任何人都可以澄清每种情况会被使用吗?
-
$('.menu')
…选择class='menu'
元素 -
$('menu')
…..选择元素
-
$('#menu')
…选择id='menu'
的元素
1st找到
第二个找到
3rd找到
请注意,这些规则适用并基于CSS。
$(’。menu’):所有带有类菜单的元素
$(’menu’):所有菜单元素
$(’#menu’):具有id菜单的元素
$('.menu') -> or any other tag with class menu $('menu') -> $('#menu') -> or any other tag with id menu
类选择器(“.class”)
选择具有给定类的所有元素。
元素选择器(“元素”)
选择具有给定标记名称的所有元素。
ID选择器(“#id”)
选择具有给定id属性的单个元素。
参考: http : //api.jquery.com/category/selectors/basic-css-selectors/
jQuery选择器语法与css相同。 因此“.menu”将使用一类菜单选择所有内容,“#menu”将选择具有id菜单的对象(应该只有一个!“menu”将尝试选择类型菜单的元素。
一个例子;
Div 1 Div 2 Span 1 Span 2 $(".foo").css("background", "red"); //sets the background of all 4 elements to red $("div").css("background", "blue"); //sets the background of the two divs to blue $("#s1").css("background", "green"); //sets the background of span 1 to green
摘自http://forum.codecall.net/javascript-tutorials/14363-jquery-selectors.html
#id: -> This will match any element with the given ID. element -> This will match any element supplied. .class -> This will match any element with the given class.