选择器 Selectors
基础
#idSelects a single element with the given id attribute.
elementSelects all elements with the given tag name.
层级
parent > childSelects all direct child elements specified by "child" of elements specified by "parent".
prev + nextSelects all next elements matching "next" that are immediately preceded by a sibling "prev".
prev ~ siblingsSelects all sibling elements that follow after the "prev" element, have the same parent, and match the filtering "siblings" selector.
简单过滤器
:firstSelects the first matched element.
:lastSelects the last matched element.
:not(selector)Selects all elements that do not match the given selector.
:evenSelects even elements, zero-indexed.
:oddSelects odd elements, zero-indexed.
:eq(index)Select the element at index n within the matched set.
:gt(index)Select all elements at an index greater than index within the matched set.
:lt(index)Select all elements at an index less than index within the matched set.
:headerSelects all elements that are headers, like h1, h2, h3 and so on.
:animatedSelect all elements that are in the progress of an animation at the time the selector is run.
内容过滤器
:emptySelect all elements that have no children (including text nodes).
:has(selector)Selects elements which contain at least one element that matches the specified selector.
:parentSelect all elements that are the parent of another element, including text nodes.
可见性过滤器
:hiddenSelects all elements that are hidden.
:visibleSelects all elements that are visible.
子元素过滤器
:first-childSelects all elements that are the first child of their parent.
:last-childSelects all elements that are the last child of their parent.
:only-childSelects all elements that are the only child of their parent.
属性过滤器
[attribute]Selects elements that have the specified attribute, with any value.
[attribute=value]Selects elements that have the specified attribute with a value exactly equal to a certain value.
[attribute!=value]Select elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value.
[attribute^=value]Selects elements that have the specified attribute with a value beginning exactly with a given string.
[attribute$=value]Selects elements that have the specified attribute with a value ending exactly with a given string.
[attribute*=value]Selects elements that have the specified attribute with a value containing the a given substring.
[attribute|=value]Selects elements that have the specified attribute with a value either equal to a given string or starting with that string followed by a hyphen (-).
[attribute~=value]Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.
表单
:inputSelects all input, textarea, select and button elements.
:textSelects all elements of type text.
:passwordSelects all elements of type password
:radioSelects all elements of type radio.
:checkboxSelects all elements of type checkbox.
:submitSelects all elements of type submit.
:imageSelects all elements of type image.
:resetSelects all elements of type reset.
:buttonSelects all button elements and elements of type button.
:fileSelects all elements of type file.
表单过滤器
:enabledSelects all elements that are enabled
:disabledSelects all elements that are disabled.
:checkedMatches all elements that are checked.
:selectedSelects all elements that are selected.
Legend
Conventional signs
[obj] — array of objects
1.4+ — new in 1.4
1.4∗ — changed in 1.4
Data types
* — anything
$ — jQuery object
arr — array
bool — boolean
el — DOM element
fn — function
int — integer
obj — object
str — string
XHR — XMLHttpRequest
|
核心
jQuery函数
$. jQuery( selector [, context] ), . jQuery( element ), . jQuery( elementArray ), . jQuery( jQueryObject ), . jQuery( )1.4∗Accepts a string containing a CSS selector which is then used to match a set of elements.
$. jQuery( html [, ownerDocument] ), . jQuery( html, props )1.4∗Creates DOM elements on the fly from the provided string of raw HTML.
$. jQuery( fn )Binds a function to be executed when the DOM has finished loading.
jQuery对象访问器
$.each( fn(index, element) )Iterate over a jQuery object, executing a function for each matched element.
num.size( )Return the number of DOM elements matched by the jQuery object. , .lengthThe number of elements in the jQuery object.
str. selectorA selector representing selector originally passed to jQuery().
el. contextThe DOM node context originally passed to jQuery(); if none was passed then context will likely be the document.
$. eq( index )Reduce the set of matched elements to the one at the specified index.
jQuery.error( str )1.4+Takes a string and throws an exception containing it.
[el],el. get( [index] )Retrieve the DOM elements matched by the jQuery object.
num. index( )1.4∗, . index( selector )1.4∗, . index( element )Search for a given element from among the matched elements.
$jQuery.pushStack( elements, [name, arguments] )Add a collection of DOM elements onto the jQuery stack.
arr. toArray( )1.4+Retrieve all the DOM elements contained in the jQuery set, as an array.
属性 Attributes
属性 Attributes
str. attr( name )Get the value of an attribute for the first element in the set of matched elements.
$. attr( name, val ), . attr( map ), . attr( name, fn(index, attr) )Set one or more attributes for the set of matched elements.
$. removeAttr( name )Remove an attribute from each element in the set of matched elements.
样式类 Class
$. addClass( class ), . addClass( fn(index, class) )1.4∗Adds the specified class(es) to each of the set of matched elements.
bool. hasClass( class )Determine whether any of the matched elements are assigned the given class.
$. removeClass( [class] ), . removeClass( fn(index, class) )1.4∗Remove one or all classes from each element in the set of matched elements.
$. toggleClass( class [, switch] ), . toggleClass( fn(index, class) [, switch] )1.4∗Add or remove a class from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.
HTML, text
str. html( )Get the HTML contents of the first element in the set of matched elements.
$. html( val ), . html( fn(index, html) )1.4∗Set the HTML contents of each element in the set of matched elements.
str. text( )Get the combined text contents of each element in the set of matched elements, including their descendants.
$. text( val ), . text( fn(index, html) )1.4∗Set the content of each element in the set of matched elements to the specified text.
值 Value
str,arr. val( )Get the current value of the first element in the set of matched elements.
$. val( val ), . val( fn() )1.4∗Set the value of each element in the set of matched elements.
CSS
CSS
str. css( name )Get the value of a style property for the first element in the set of matched elements.
$. css( name, val ), . css( map ), . css( name, fn(index, val) )1.4∗Set one or more CSS properties for the set of matched elements.
位置
obj. offset( )Get the current coordinates of the first element in the set of matched elements, relative to the document.
$. offset( coord ), . offset( fn( index, coord ) )1.4+Set the current coordinates of every element in the set of matched elements, relative to the document.
$. offsetParent( )Get the closest ancestor element that is positioned.
obj. position( )Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.
int. scrollTop( )Get the current vertical position of the scroll bar for the first element in the set of matched elements.
$. scrollTop( val )Set the current vertical position of the scroll bar for each of the set of matched elements.
int. scrollLeft( )Get the current horizontal position of the scroll bar for the first element in the set of matched elements.
$. scrollLeft( val )Set the current horizontal position of the scroll bar for each of the set of matched elements.
宽高
int. height( )Get the current computed height for the first element in the set of matched elements.
$. height( val ), . height( fn(index, height ) )1.4∗Set the CSS height of every matched element.
int. width( )Get the current computed width for the first element in the set of matched elements.
$. width( val ), .width(( fn(index, height ) )1.4∗Set the CSS width of each element in the set of matched elements.
int. innerHeight( )Get the current computed height for the first element in the set of matched elements, including padding but not border.
int. innerWidth( )Get the current computed width for the first element in the set of matched elements, including padding but not border.
int. outerHeight( [margin] )Get the current computed height for the first element in the set of matched elements, including padding and border.
int. outerWidth( [margin] )Get the current computed width for the first element in the set of matched elements, including padding and border.
|
遍历
筛选
$. eq( index )Reduce the set of matched elements to the one at the specified index.
$. first( )1.4+Reduce the set of matched elements to the first in the set.
$. last( )1.4+Reduce the set of matched elements to the final one in the set.
$. has( selector ), . has( element )1.4+Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.
$. filter( selector ), . filter( fn(index) )Reduce the set of matched elements to those that match the selector or pass the function's test.
bool. is( selector )Check the current matched set of elements against a selector and return true if at least one of these elements matches the selector.
$. map( fn(index, element) )Pass each element in the current matched set through a function, producing a new jQuery object containing the return values..
$. not( selector ), . not( elements ), . not( fn( index ) )Remove elements from the set of matched elements.
$. slice( start [, end] )Reduce the set of matched elements to a subset specified by a range of indices.
树遍历
$. children( [selector] )Get the children of each element in the set of matched elements, optionally filtered by a selector.
$. closest( selector [, context] )1.4∗Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
arr. closest( selectors [, context] )1.4+Gets an array of all the elements and selectors matched against the current element up through the DOM tree.
$. find( selector )Get the descendants of each element in the current set of matched elements, filtered by a selector.
$. next( [selector] )Get the immediately following sibling of each element in the set of matched elements, optionally filtered by a selector.
$. nextAll( [selector] )Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.
$. nextUntil( [selector] )1.4+Get all following siblings of each element up to but not including the element matched by the selector.
$. offsetParent( )Get all following siblings of each element up to but not including the element matched by the selector.
$. parent( [selector] )Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
$. parents( [selector] )Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.
$. parentsUntil( [selector] )1.4+Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.
$. prev( [selector] )Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.
$. prevAll( [selector] )Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.
$. prevUntil( [selector] )1.4+Get all preceding siblings of each element up to but not including the element matched by the selector.
$. siblings( [selector] )Get the siblings of each element in the set of matched elements, optionally filtered by a selector.
其他遍历
$. add( selector [, context] ), . add( elements ), . add( html )1.4∗Add elements to the set of matched elements.
$. andSelf( )Add the previous set of elements on the stack to the current set.
$. contents( )Get the children of each element in the set of matched elements, including text nodes.
$. end( )End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.
操作 Manipulation
内部插入
$. append( content ), . append( fn( index, html ) )1.4∗Insert content, specified by the parameter, to the end of each element in the set of matched elements.
$. appendTo( target )Insert every element in the set of matched elements to the end of the target.
$. prepend( content ), . prepend( fn( index, html ) )1.4∗Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.
$. prependTo( target )Insert every element in the set of matched elements to the beginning of the target.
外部插入
$. after( content ), . after( fn() )1.4∗ Insert content, specified by the parameter, after each element in the set of matched elements.
$. before( content ), . before( fn() )1.4∗Insert content, specified by the parameter, before each element in the set of matched elements.
$. insertAfter( target )Insert every element in the set of matched elements after the target.
$. insertBefore( target )Insert every element in the set of matched elements before the target.
包裹
$. unwrap( )1.4+Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
$. wrap( wrappingElement ), . wrap( fn )1.4∗Wrap an HTML structure around each element in the set of matched elements.
$. wrapAll(wrappingElement ), . wrapAll( fn )1.4∗Wrap an HTML structure around all elements in the set of matched elements.
$. wrapInner( wrappingElement ), . wrapInner( fn )1.4∗Wrap an HTML structure around the content of each element in the set of matched elements.
替换
$. replaceWith( content ), . replaceWith( fn )1.4∗Replace each element in the set of matched elements with the provided new content.
$. replaceAll( selector )Replace each target element with the set of matched elements.
删除
$. detach( [selector] )1.4+Remove the set of matched elements from the DOM.
$. empty( )Remove all child nodes of the set of matched elements from the DOM.
$. remove( [selector] )Remove the set of matched elements from the DOM.
复制
$. clone( [withDataAndEvents] )Create a copy of the set of matched elements.
|
事件 Events
页面加载
$. ready( fn() )Specify a function to execute when the DOM is fully loaded.
事件处理
$. bind( type [, data ], fn(eventObj) )Attach a handler to an event for the elements.
$. bind( type [, data], false )1.4∗Attach a handler to an event for the elements.
$. bind( array )1.4∗Attach a handler to an event for the elements.
$. unbind( [type] [, fn])Remove a previously-attached event handler from the elements.
$. one( type [, data ], fn(eventObj) )Attach a handler to an event for the elements. The handler is executed at most once.
$. trigger( event [, data])Execute all handlers and behaviors attached to the matched elements for the given event type.
obj. triggerHandler( event [, data])Execute all handlers attached to an element for an event.
$. delegate( selector, type, [data], handler) 1.4+Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.
$. undelegate( [selector, type, [handler]]) 1.4+Remove a handler from the event for all elements which match the current selector, now or in the future, based upon a specific set of root elements.
事件委派
$. live( eventType [, data], fn() )Attach a handler to the event for all elements which match the current selector, now or in the future.
$. die( )1.4+, . die( [eventType] [, fn() ])Remove an event handler previously attached using .live() from the elements.
事件切换
$. hover( fnIn(eventObj), fnOut(eventObj))Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.
$. toggle( fn(eventObj), fn2(eventObj) [, ...])Bind two or more handlers to the matched elements, to be executed on alternate clicks.
事件
function ( [data,] [fn] )
事件对象
event = {
elcurrentTarget, The current DOM element within the event bubbling phase.
*data, Contains the optional data passed to jQuery.fn.bind when the current executing handler was bound.
boolisDefaultPrevented(), Returns whether event.preventDefault() was ever called on this event object.
boolisPropagationStopped(), Returns whether event.stopPropagation() was ever called on this event object.
strnamespace, This attribute contains the namespace specified when the event was triggered. 1.4+
numpageX, The mouse position relative to the left edge of the document.
numpageY, The mouse position relative to the top edge of the document.
preventDefault(), If this method is called, the default action of the event will not be triggered.
elrelatedTarget, The other DOM element involved in the event, if any.
objresult, This attribute contains the last value returned by an event handler that was triggered by this event, unless the value was undefined.
stopPropagation(), This method prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
eltarget, The DOM element that initiated the event.
numtimeStamp, This attribute returns the number of milliseconds since January 1, 1970, when the event is triggered.
strtype, Describes the nature of the event.
strwhichFor key or button events, this attribute indicates the specific button or key that was pressed.
}
效果
基本1.4∗
$. show( [ duration [, easing] [, fn] ] )Display the matched elements.
$. hide( [ duration [, easing] [, fn] ] )Hide the matched elements.
$. toggle( [showOrHide] )Bind two or more handlers to the matched elements, to be executed on alternate clicks.
$. toggle( duration [, easing] [, fn] )Bind two or more handlers to the matched elements, to be executed on alternate clicks.
滑动1.4∗
$. slideDown( duration [, easing] [, fn] )Display the matched elements with a sliding motion.
$. slideUp( duration [, easing] [, fn] )Hide the matched elements with a sliding motion.
$. slideToggle( [duration] [, easing] [, fn] )Display or hide the matched elements with a sliding motion.
淡入淡出1.4∗
$. fadeIn( duration [, easing] [, fn] )Display the matched elements by fading them to opaque.
$. fadeOut( duration [, easing] [, fn] )Hide the matched elements by fading them to transparent.
$. fadeTo( [duration,] opacity [, easing] [, fn] )Adjust the opacity of the matched elements.
$. fadeToggle( [duration,] [, easing] [, fn] )Display or hide the matched elements by animating their opacity.
自定义
$. animate( params [, duration] [, easing] [, fn] )Perform a custom animation of a set of CSS properties.
$. animate( params, options )Perform a custom animation of a set of CSS properties.
$. stop( [clearQueue] [, jumpToEnd] )Stop the currently-running animation on the matched elements.
$. delay( duration [, queueName] )1.4+Set a timer to delay execution of subsequent items in the queue.
|
AJAX
底层接口
XHRjQuery.ajax( options )
- boolasync = true
By default, all requests are sent asynchronous (i.e. this is set to true by default).
- fnbeforeSend( XHR )
A pre-callback to modify the XMLHttpRequest object before it is sent.
- boolcache = true
If set to false it will force the pages that you request to not be cached by the browser.
- fncomplete(XHR, status)
A function to be called when the request finishes (after success and error callbacks are executed).
- strcontentType
When sending data to the server, use this content-type. Default is "application/x-www-form-urlencoded", which is fine for most cases.
- objcontext
This object will be made the context of all Ajax-related callbacks.
- obj, strdata
Data to be sent to the server. It is converted to a query string, if not already a string.
- fndataFilter( data, type )
A function to be used to handle the raw responsed data of XMLHttpRequest.This is a pre-filtering function to sanitize the response.
- boolglobal = true
Whether to trigger global Ajax event handlers for this request.
- boolifModified = false
Allow the request to be successful only if the response has changed since the last request.
- strjsonp
Override the callback function name in a jsonp request.
- fnjsonpCallback
Specify the callback function name for a jsonp request.
- strpassword
A password to be used in response to an HTTP access authentication request.
- boolprocessData = true
By default, data passed in to the data option as an object will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded".
- numtimeout
Set a local timeout (in milliseconds) for the request.
- strtype = 'GET'
The type of request to make ("POST" or "GET"), default is "GET".
- strurl = curr. page
A string containing the URL to which the request is sent.
- strusername
A username to be used in response to an HTTP access authentication request.
- fnxhr
Callback for creating the XMLHttpRequest object.
- strscriptCharset
Forces the request to be interpreted as a certain charset.
- strdataType ∈ {xml, json, script, html}
The type of data that you're expecting back from the server.
- fnerror( XHR, status, errorThrown )
A function to be called if the request fails.
- fnsuccess( data, status, XHR )
A function to be called if the request succeeds.
快捷方法
$. load( url [, data] [, fn( responseText, status, XHR )] )Load data from the server and place the returned HTML into the matched element.
XHRjQuery.get( url [, data] [, fn( data, status, XHR )] [, type] )Load data from the server using a HTTP GET request.
XHRjQuery.getJSON( url [, data] [, fn( data, status )] )Load JSON-encoded data from the server using a GET HTTP request.
XHRjQuery.getScript( url [, fn( data, status )] )Load a JavaScript file from the server using a GET HTTP request, then execute it.
XHRjQuery.post( url [, data] [, fn( data, status )] [, type] )Load data from the server using a HTTP POST request.
全局Ajax事件
$. ajaxComplete( fn( event, XHR, options ) )Register a handler to be called when Ajax requests complete.
$. ajaxError( fn( event, XHR, options, thrownError ) )Register a handler to be called when Ajax requests complete with an error.
$. ajaxSend( fn( event, XHR, options ) )Show a message before an Ajax request is sent.
$. ajaxStart( fn( ) )Register a handler to be called when the first Ajax request begins.
$. ajaxStop( fn( ) )Hide a loading message after all the Ajax requests have stopped.
$. ajaxSuccess( fn(event, XHR, options) )Show a message when an Ajax request completes successfully.
辅助函数
str. serialize( )Encode a set of form elements as a string for submission.
[obj]. serializeArray( )Encode a set of form elements as an array of names and values.
strjQuery.param( obj, [traditional] )1.4∗Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.
工具
浏览器及特性检测
objjQuery.supportA collection of properties that represent the presence of different browser features or bugs.
objjQuery.browserdeprecatedContains flags for the useragent, read from navigator.userAgent.
booljQuery.boxModeldeprecatedStates if the current page, in the user's browser, is being rendered using the W3C CSS Box Model.
基本操作
objjQuery.each( obj, fn( index, valueOfElement ) )Iterates through the array displaying each number as both a word and numeral.
objjQuery.extend( [deep,] target, obj1 [, objN] )Merge the contents of two or more objects together into the first object.
arrjQuery.grep( array, fn( element, index ) [, invert] )Finds the elements of an array which satisfy a filter function. The original array is not affected.
arrjQuery.makeArray( obj )Convert an array-like object into a true JavaScript array.
arrjQuery.map( array, fn( element, index ) )Translate all items in an array or array-like object to another array of items.
numjQuery.inArray( val, array )Search for a specified value within an array and return its index (or -1 if not found).
arrjQuery.merge( first, second )Merge the contents of two arrays together into the first array.
fnjQuery.proxy( fn, scope ), jQuery.proxy( scope, name )1.4+Takes a function and returns a new one that will always have a particular context.
arrjQuery.unique( array )Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.
strjQuery.trim( str )Remove the whitespace from the beginning and end of a string.
objjQuery.parseJSON( str )1.4+Takes a well-formed JSON string and returns the resulting JavaScript object.
数据功能
$. clearQueue( [name] )1.4+Remove from the queue all items that have not yet been run.
$. dequeue( [name] ), jQuery.dequeue( [name] )Execute the next function on the queue for the matched elements.
obj. data( ), . data( key )Store arbitrary data associated with the specified element.
$. data( key, val ), . data( obj )1.4∗Store arbitrary data associated with the specified element.
$. removeData( [name] )Remove a previously-stored piece of data.
[fn]. queue( [name] ) jQuery.queue( [name] )Show the queue of functions to be executed on the matched elements.
$. queue( [name,] fn( next ) ), jQuery.queue( [name,] fn( ) )Show the queue of functions to be executed on the matched elements.
$. queue( [name,] queue ), jQuery.queue( [name,] queue )Show the queue of functions to be executed on the matched elements.
测试操作
strjQuery.type( obj )1.4+Determine the internal JavaScript [[Class]] of an object.
booljQuery.isFunction( obj )Determine if the argument passed is a Javascript function object.
booljQuery.isPlainObject( obj )1.4+Check to see if an object is a plain object (created using "{}" or "new Object").
|