jQuery 获取 DOM 元素

jQuery 支持多数 CSS3 选择器,本身有扩展;完整参考链接

  • $() 将会返回一个 jQuery 对象
$( "#myId" ); 
$( ".myClass" );
$( "input[name='first_name']" );  
$( "#contents ul.people li" );
$( "div.myClass, ul.people" );   

pseudo-selectors

$( "a.external:first" );
$( "tr:odd" );
 
// Select all input-like elements in a form (more on this below).
$( "#myForm :input" );
$( "div:visible" );
 
// All except the first three divs.
$( "div:gt(2)" );
 
// All currently animated divs.
$( "div:animated" );

过滤器

// Refining selections.
$( "div.foo" ).has( "p" );         // div.foo elements that contain 

tags $( "h1" ).not( ".bar" ); // h1 elements that don't have a class of bar $( "ul li" ).filter( ".current" ); // unordered list items with class of current $( "ul li" ).first(); // just the first unordered list item $( "ul li" ).eq( 5 ); // the sixth 索引值从 0 开始

选择表单元素

$( "form :checked" ); /* 已被选择的 checkbox, radio button,