jQuery 速查表
可搜尋、可列印的 jQuery 參考手冊——選擇器、DOM 操作、事件、特效、走訪、AJAX 以及表單輔助方法。免費。
Selectors
14$('#id')
Select an element by its id
$('.class')
Select all elements with a class
$('div')
Select all elements of a tag name
$('*')
Select every element in the page
$('[name="email"]')
Select by an attribute value
$('a[href^="https"]')
Attribute starts-with selector
$('ul li:first')
First matched element
$('ul li:last')
Last matched element
$('tr:eq(2)')
Element at a zero-based index
$('li:not(.done)')
Exclude elements matching a selector
$(':checked')
All checked checkboxes/radios
$('div:visible')
Only currently visible elements
$('ul li')
Descendant combinator
$('ul > li')
Direct child combinator
Document ready & DOM
10$(function () { /* ... */ });
Run code once the DOM is ready
$(document).ready(fn)
Explicit document-ready handler
$(window).on('load', fn)
Run after all assets have loaded
$(this)
Wrap the current context element
$(el)
Wrap a raw DOM node in jQuery
$('<div class="box"></div>')
Create a new element from HTML
$('#box')[0]
Get the underlying DOM node
$('#box').get(0)
Get the DOM node via .get()
$('.item').length
Count the matched elements
$('#box').is(':visible')
Test a selector against the set
DOM manipulation
12$('#box').html('<b>Hi</b>')
Set the inner HTML
$('#box').text('Hello')
Set the text content (escaped)
$('#name').val('Ada')
Set a form field value
$('#list').append('<li>x</li>')
Insert content at the end
$('#list').prepend('<li>x</li>')
Insert content at the start
$('#box').before('<hr>')
Insert content before the element
$('#box').after('<hr>')
Insert content after the element
$('#box').remove()
Remove the element from the DOM
$('#box').empty()
Remove all child nodes
$('#box').clone()
Create a copy of the element
$('#box').wrap('<div>')
Wrap the element in new HTML
$('#box').replaceWith('<p>')
Replace the element entirely
Attributes & CSS
13$('#img').attr('src')
Get an attribute value
$('#img').attr('alt', 'Logo')
Set an attribute value
$('#cb').prop('checked')
Get a DOM property (checked/disabled)
$('#cb').prop('checked', true)
Set a DOM property
$('#img').removeAttr('title')
Remove an attribute
$('.box').addClass('active')
Add a CSS class
$('.box').removeClass('active')
Remove a CSS class
$('.box').toggleClass('open')
Toggle a CSS class on/off
$('.box').hasClass('active')
Test if a class is present
$('#box').css('color', 'red')
Set a single CSS property
$('#box').css({top: 0, left: 0})
Set multiple CSS properties
$('#box').width()
Get the computed width in pixels
$('#box').height()
Get the computed height in pixels
Events
12$('#btn').on('click', fn)
Attach an event handler
$('#btn').off('click')
Detach event handlers
$('#btn').click(fn)
Shorthand click handler
$('#form').submit(fn)
Handle form submission
$('#sel').change(fn)
Handle value changes
$('#in').keyup(fn)
Handle key-up in a field
$('#list').on('click', '.item', fn)
Delegated event for dynamic children
$('#btn').trigger('click')
Programmatically fire an event
$('#btn').one('click', fn)
Run a handler at most once
e.preventDefault()
Stop the default browser action
e.stopPropagation()
Stop the event from bubbling
e.target
The element that triggered the event
Effects & animation
12$('#box').show()
Display a hidden element
$('#box').hide()
Hide an element
$('#box').toggle()
Toggle visibility
$('#box').fadeIn(300)
Fade an element in
$('#box').fadeOut(300)
Fade an element out
$('#box').fadeTo(300, 0.5)
Fade to a given opacity
$('#box').slideUp()
Slide an element up (collapse)
$('#box').slideDown()
Slide an element down (expand)
$('#box').slideToggle()
Toggle a slide animation
$('#box').animate({left: 100})
Animate CSS properties
$('#box').stop()
Stop the current animation
$('#box').delay(500).fadeIn()
Delay the next queued effect
Traversing
12$('#box').find('.item')
Find descendants matching a selector
$('#box').parent()
Get the direct parent
$('#box').parents('.row')
Get all ancestors matching a selector
$('#box').closest('.row')
Nearest matching ancestor (or self)
$('#box').children()
Get the direct children
$('#box').siblings()
Get sibling elements
$('#box').next()
Get the next sibling
$('#box').prev()
Get the previous sibling
$('.item').each(fn)
Iterate over each matched element
$('.item').filter('.on')
Reduce the set by a selector
$('.item').first()
Reduce the set to the first element
$('.item').eq(1)
Reduce the set to one index
AJAX
11$.ajax({url: '/api', method: 'GET'})
Full-control AJAX request
$.get('/api', fn)
Shorthand HTTP GET request
$.post('/api', data, fn)
Shorthand HTTP POST request
$.getJSON('/api.json', fn)
GET request expecting JSON
$('#box').load('/part.html')
Load HTML into an element
$.ajax({dataType: 'json'})
Set the expected response type
$.ajax({data: {q: 'x'}})
Send request parameters
.done(function (res) {})
Run a callback on success
.fail(function (err) {})
Run a callback on failure
.always(function () {})
Run a callback after completion
$.ajaxSetup({headers: {}})
Set defaults for all requests
Forms
10$('#form').serialize()
Encode form data as a query string
$('#form').serializeArray()
Encode form data as an array
$('#name').val()
Read a field value
$('#name').val('Ada')
Write a field value
$(':input')
Select all form input elements
$('#cb').is(':checked')
Test if a checkbox is checked
$('#sel option:selected')
Get the selected option
$('#in').focus()
Move focus to a field
$('#form').on('submit', fn)
Handle the form submit event
$('#in').prop('disabled', true)
Disable a form control
Utilities
11$.each(arr, fn)
Iterate over an array or object
$.map(arr, fn)
Build a new array from each item
$.grep(arr, fn)
Filter an array by a predicate
$.extend({}, a, b)
Merge objects into a target
$.trim(' hi ')
Trim leading/trailing whitespace
$.inArray(2, arr)
Find an item index in an array
$.isArray(x)
Test whether a value is an array
$.isFunction(x)
Test whether a value is a function
$.parseJSON(str)
Parse a JSON string into an object
$.now()
Current timestamp in milliseconds
$.type(x)
Get the internal type of a value
沒有條目符合「:q」。
關於 jQuery 速查表
這份 jQuery 小抄是這個至今仍支撐大量現有網站的函式庫的精簡參考資料:選擇器、文件就緒與 DOM 基礎、DOM 操作、屬性與 CSS、事件、特效與動畫、走訪、AJAX、表單,以及工具函式。
如果你維護舊有的前端、外掛或較舊的 CMS 佈景主題,熟悉 jQuery 仍是實用技能。每一列都將方法呼叫與一行說明配對,讓你不必翻閱舊文件,就能想起事件委派、鏈式走訪或 AJAX 請求的確切寫法。
此小抄免費且完全在用戶端運作。用搜尋框即時篩選方法,透過固定式目錄在各章節間跳轉,一鍵複製任何程式碼片段,並可在深陷舊系統重構工作時列印本頁。
如何使用 jQuery 速查表
- 瀏覽各章節,從選擇器、文件就緒與 DOM,經走訪,到 AJAX 與工具函式。
- 搜尋 on、closest 或 ajax 等方法,即可即時篩選小抄內容。
- 透過固定式側邊欄跳到特效與動畫等章節。
- 點擊程式碼片段或其複製圖示,將 jQuery 呼叫複製到你的程式碼中。
- 處理舊系統專案時,可列印本頁作為離線參考資料。
常見問題
共十個章節:選擇器、文件就緒與 DOM 基礎、DOM 操作、屬性與 CSS、事件、特效與動畫、走訪、AJAX、表單,以及工具輔助函式。
對新專案而言,大多數團隊會使用原生 JavaScript,但仍有大量正式上線的程式碼、外掛與 CMS 佈景主題在使用 jQuery——這份小抄的目的正是幫助你有信心地維護與遷移那些程式碼。
有的。事件章節包含了 .on() 的委派用法,以及綁定、解除綁定與觸發事件的方式——這正是處理動態插入元素時最需要用到的模式。
可以。點擊任何程式碼片段,或其列上的複製圖示,該呼叫就會複製到剪貼簿,隨時可貼上使用。
是的,完全免費——可搜尋、可列印,並在瀏覽器中呈現,不需註冊。
熱門搜尋
jquery cheat sheet
jquery selectors
jquery ajax syntax
jquery event methods
jquery dom manipulation
jquery effects and animation
jquery traversing methods
jquery reference
需要協助?
使用此工具時遇到問題?請告訴我們的團隊。