Components
5 methodsBootstrap JavaScript 组件的编程式 API。每个组件是 bootstrap 命名空间上的类(如 bootstrap.Modal)。
$(selector, context?) -> jQuery为 modal 元素创建 Modal 实例。使用 show/hide/toggle 控制。
Parameters
| Name | Type | Description |
|---|---|---|
| selector | string | CSS selector. |
| context | Element | jQuery | Optional root to search within. |
Returns
jQuery
Example
$('li.item').each(function (i, el) {
console.log(i, el.textContent);
});$(element) / $(htmlString) -> jQuery静态方法,返回元素的现有组件实例,或创建新实例。
Parameters
| Name | Type | Description |
|---|---|---|
| element | Element | string | DOM element or HTML string. |
Returns
jQuery
Example
const $el = $('<div class="x">hi</div>');
$('body').append($el);$.each(arrayOrObject, callback)显示、 隐藏或切换组件实例。触发相应的前后事件。
Parameters
| Name | Type | Description |
|---|---|---|
| arrayOrObject | Array | object | Collection to iterate. |
| callback | (index, value) => boolean | void | Function called per item. |
Returns
Array | object
Example
$.each([10, 20, 30], function (i, v) {
console.log(i, v);
});$.extend(deep?, target, ...objects)销毁组件实例,移除其 DOM 产物和事件监听器。
Parameters
| Name | Type | Description |
|---|---|---|
| deep | boolean | Optional; if true, merge recursively. |
| target | object | Object to merge into. |
| objects | object | Additional objects to merge. |
Returns
object
Example
const merged = $.extend({}, { a: 1 }, { b: 2 });
// { a: 1, b: 2 }$(callback)更新组件的位置/内容(由 tooltip、popover、dropdown 使用)。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | () => void | Function to run on DOM ready. |
Returns
jQuery
Example
$(function () {
console.log('DOM is ready');
});AJAX
4 methodsjQuery AJAX helpers for making HTTP requests and handling responses.
$.ajax(url, settings?) -> jqXHRPerforms an asynchronous HTTP request. Returns a Promise-like jqXHR object.
Parameters
| Name | Type | Description |
|---|---|---|
| url | string | Request URL. |
| settings | object | Optional: method, data, headers, success/error. |
Returns
jqXHR (Promise)
Example
$.ajax('/api/users', { method: 'GET' })
.done(function (data) { console.log(data); })
.fail(function (err) { console.error(err); });$.get(url, data?, success?, dataType?) -> jqXHRShorthand for an HTTP GET request.
Parameters
| Name | Type | Description |
|---|---|---|
| url | string | Request URL. |
| data | object | string | Optional query params. |
| success | (data, textStatus, jqXHR) => void | Optional success callback. |
Returns
jqXHR (Promise)
Example
$.get('/api/users/1', function (user) {
console.log(user);
});$.post(url, data?, success?, dataType?) -> jqXHRShorthand for an HTTP POST request.
Parameters
| Name | Type | Description |
|---|---|---|
| url | string | Request URL. |
| data | object | string | Optional request body. |
Returns
jqXHR (Promise)
Example
$.post('/api/users', { name: 'Sam' })
.done(function (u) { console.log(u); });$.getJSON(url, data?, success?) -> jqXHRLoads JSON-encoded data from the server using a GET HTTP request.
Returns
jqXHR (Promise)
Example
$.getJSON('/api/users', function (users) {
console.log(users.length);
});DOM
5 methodsjQuery DOM manipulation, traversal, and event methods.
.html(htmlString?) -> string | jQueryGets the HTML contents of the first element, or sets the HTML of every matched element.
Parameters
| Name | Type | Description |
|---|---|---|
| htmlString | string | Optional HTML to set. |
Returns
string | jQuery
Example
$('#box').html('<b>hi</b>');
const html = $('#box').html();.text(text?) -> string | jQueryGets or sets the combined text contents of matched elements.
Returns
string | jQuery
Example
$('#box').text('Hello');
const t = $('#box').text();.on(events, handler) -> jQueryAttaches an event handler for one or more events to the selected elements.
Parameters
| Name | Type | Description |
|---|---|---|
| events | string | Space-separated event names, e.g. 'click change'. |
| handler | (eventObject) => false | void | Handler function. |
Returns
jQuery
Example
$('button').on('click', function (e) {
console.log('clicked', e.target);
});.find(selector) -> jQueryGets the descendants of each element filtered by a selector.
Returns
jQuery
Example
$('ul').find('li.active').each(function () {
console.log(this);
});.css(propertyName, value?) -> string | jQueryGets or sets CSS properties for matched elements.
Parameters
| Name | Type | Description |
|---|---|---|
| propertyName | string | object | CSS property name or object of property/value pairs. |
| value | string | number | Optional value to set. |
Returns
string | jQuery
Example
$('#box').css('color', 'red');
$('#box').css({ background: '#eee', padding: '8px' });