主题
Getting Started
Including jQuery
javascript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Official CDN
javascript
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
jQuery syntax
javascript
$(selector).methodOrFunction();
Example:
javascript
$('#menu').on('click', () =>{
$(this).hide();
});
$("body").css("background", "red");
jQuery document ready
javascript
$(document).ready(function() {
// Runs after the DOM is loaded.
alert('DOM fully loaded!');
});
javascript
$(function(){
// Runs after the DOM is loaded.
alert('DOM fully loaded!');
});
jQuery Selectors
Examples
javascript
$("button").click(() => {
$(":button").css("color", "red");
});
Combining selectors
javascript
$("selector1, selector2 ...selectorn")
Basics
- *
- .class
- element
- #id
- :hidden
- :visible
- :contains()
- :empty
- :has()
- :parent
- parent > child
- ancestor descendant
- prev + next
- prev ~ siblings
Basic Filters
- :animated
- :eq()
- :even
- :first
- :gt()
- :header
- :lang()
- :last
- :lt()
- :not()
- :odd
- :root
- :target{data-tooltip="Selects the target element indicated by the fragment identifier of the document's URI."}
Attribute
- [name|="value"]
- [name*="value"]
- [name~="value"]
- [name$="value"]
- [name="value"]
- [name!="value"]{data-tooltip="Select elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value."}
- [name^="value"]
- [name]
- [name="value"][name2="value2"]
Child Filters
- :first-child
- :first-of-type
- :last-child
- :last-of-type
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-child
- :only-of-type()
Forms
- :button
- :checkbox
- :checked
- :disabled
- :enabled
- :focus
- :file
- :image
- :input
- :password
- :radio
- :reset
- :selected
- :submit
- :text
jQuery Attributes
Examples
javascript
$('h2').css({
color: 'blue',
backgroundColor: 'gray',
fontSize: '24px'
});
jQuery addClass
javascript
$('.button').addClass('active');
jQuery removeClass
javascript
$('.button').on('mouseleave', evt => {
let e = evt.currentTarget;
$(e).removeClass('active');
});
jQuery .toggleClass
javascript
$('.choice').toggleClass('highlighted');
Attributes
Data
CSS
- .addClass()
- .hasClass()
- .removeClass()
- .toggleClass(){data-tooltip="Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument."}
- .css()
- jQuery.cssHooks
- jQuery.cssNumber
- jQuery.escapeSelector()
Dimensions
Offset
jQuery Manipulation
Examples
javascript
/*<span>Span.</span>*/
$('span').after('<p>Paragraph.</p>');
/*<span>Span.</span><p>Paragraph.</p>*/
/*<span>Span.</span>*/
$('<span>Span.</span>').replaceAll('p');
/*<p>Span.</p>*/
/*<span>This is span.</span>*/
$('span').wrap('<p></p>');
/*<p><span>This is span.</span></p>*/
Copying
DOM Insertion, Around
DOM Insertion, Inside
DOM Insertion, Outside
DOM Removal
DOM Replacement
jQuery Traversing
Filtering
- .eq()
- .filter(){data-tooltip="Reduce the set of matched elements to those that match the selector or pass the function's test. "}
- .first()
- .has()
- .is()
- .last()
- .map()
- .not()
- .slice()
Miscellaneous Traversing
Tree Traversal
- .children()
- .closest()
- .find()
- .next()
- .nextAll()
- .nextUntil()
- .parent()
- .parents()
- .parentsUntil()
- .prev()
- .prevAll()
- .prevUntil()
- .siblings()
jQuery Events
Examples
javascript
// A mouse event 'click'
$('#menu-button').on('click', () => {
$('#menu').show();
});
// A keyboard event 'keyup'
$('#textbox').on('keyup', () => {
$('#menu').show();
});
// A scroll event 'scroll'
$('#menu-button').on('scroll', () => {
$('#menu').show();
});
Event object
javascript
$('#menu').on('click', event => {
$(event.currentTarget).hide();
});
Method chaining
javascript
$('#menu-btn').on('mouseenter', () => {
$('#menu').show();
}).on('mouseleave', () => {
$('#menu').hide();
});
Prevents the event
javascript
$( "p" ).click(function( event ) {
event.stopPropagation();
// Do something
});
Browser Events
Event Object
- event.currentTarget
- event.delegateTarget
- event.data
- event.isDefaultPrevented()
- event.isImmediatePropagationStopped()
- event.isPropagationStopped()
- event.metaKey
- event.namespace
- event.pageX
- event.pageY
- event.preventDefault()
- event.relatedTarget
- event.result
- event.stopImmediatePropagation()
- event.stopPropagation()
- event.target
- event.timeStamp
- event.type
- event.which
Document Loading
Event Handler Attachment
Form Events
Keyboard Events
Mouse Events
- .click()
- .contextMenu()
- .dblclick()
- .hover()
- .mousedown()
- .mouseenter()
- .mouseleave()
- .mousemove()
- .mouseout()
- .mouseover()
- .mouseup()
- .toggle()
jQuery Effects
Examples
javascript
$('#menu-button').on('click', () => {
// $('#menu').fadeIn(400, 'swing')
$('#menu').fadeIn();
});
fadeOut effect
javascript
$('#menu-button').on('click', () => {
// $('#menu').fadeOut(400, 'swing')
$('#menu').fadeOut();
});
Basics
Sliding
Custom
- .animate()
- .clearQueue()
- .delay()
- .dequeue()
- jQuery.dequeue()
- .finish()
- jQuery.fx.interval
- jQuery.fx.off
- jQuery.speed
- .queue()
- jQuery.queue()
- .stop()
Fading
jQuery Ajax
Examples
javascript
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize()
}).done(function(server_data){
console.log("success" + server_data);
}).fail(function(jqXHR, status, err){
console.log("fail" + err);
});
Global Ajax Event Handlers
Helper Functions
Low-Level Interface
Shorthand Methods
Miscellaneous
jQuery Object
- jQuery()
- jQuery.noConflict(){data-tooltip="Relinquish jQuery's control of the $ variable."}
- jQuery.sub()
- jQuery.holdReady(){data-tooltip="Holds or releases the execution of jQuery's ready event."}
- jQuery.when()
Deferred Object
- jQuery.Deferred()
- deferred.always()
- deferred.done()
- deferred.fail()
- deferred.isRejected()
- deferred.isResolved()
- deferred.notify()
- deferred.notifyWith()
- deferred.pipe()
- deferred.progress()
- deferred.promise(){data-tooltip=" Return a Deferred's Promise object. "}
- deferred.reject()
- deferred.rejectWith()
- deferred.resolve()
- deferred.resolveWith()
- deferred.state()
- deferred.then()
- .promise()
Utilities
- jQuery.boxModel{data-tooltip="States if the current page, in the user's browser, is being rendered using the W3C CSS Box Model."}
- jQuery.browser
- jQuery.contains()
- jQuery.each(){data-tooltip="A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties."}
- jQuery.extend()
- jQuery.globalEval()
- jQuery.grep()
- jQuery.inArray()
- jQuery.isArray()
- jQuery.isEmptyObject()
- jQuery.isFunction()
- jQuery.isNumeric()
- jQuery.isPlainObject()
- jQuery.isWindow()
- jQuery.isXMLDoc()
- jQuery.makeArray()
- jQuery.map()
- jQuery.merge()
- jQuery.noop()
- jQuery.now()
- jQuery.parseHTML()
- jQuery.parseJSON()
- jQuery.parseXML()
- jQuery.proxy()
- jQuery.support{data-tooltip="A collection of properties that represent the presence of different browser features or bugs. Intended for jQuery's internal use; specific properties may be removed when they are no longer needed internally to improve page startup performance. For your own project's feature-detection needs, we strongly recommend the use of an external library such as Modernizr instead of dependency on properties in jQuery.support."}
- jQuery.trim()
- jQuery.type()
- jQuery.unique()
- jQuery.uniqueSort()