网页 综合 更新时间 2026-11-13 15:36:33

JavaScript · Bootstrap v6.0

自动蜘蛛 getbootstrap.com 正文约 6000 字
JavaScript · Bootstrap v6.0
Bring Bootstrap to life with our optional JavaScript plugins. Learn about each plugin, our data and programmatic API options, and more.
Curious which components explicitly require our JavaScript and Floating UI or Vanilla Calendar Pro?
Plugins can be included individually (using Bootstrap’s individual js/dist/*.js), or all at once using bootstrap.js or the minified bootstrap.min.js (don’t include both).
If you use a bundler (Webpack, Parcel, Vite…), you can import only the plugins you need and your bundler will automatically tree-shake the rest:
Bootstrap’s plugins are authored in TypeScript and ship with built-in type declarations — no @types/bootstrap package needed. The main entry point and every individual plugin under js/dist/*.js are fully typed, including each plugin’s configuration options:
Each plugin module also exports its configuration object type (for example TooltipConfig) if you need to type your own wrappers.
While the Bootstrap CSS can be used with any framework, the Bootstrap JavaScript is not fully compatible with JavaScript frameworks like React, Vue, and Angular which assume full knowledge of the DOM. Both Bootstrap and the framework may attempt to mutate the same DOM element, resulting in bugs like menus that are stuck in the "open" position.
A better alternative for those using this type of frameworks is to use a framework-specific package instead of the Bootstrap JavaScript. Here are some of the most popular options:
Try it yourself! Download the source code and working demo for using Bootstrap as an ES module from the twbs/examples repository. You can also open the example in StackBlitz.
Bootstrap’s JavaScript is built as native ES modules (bootstrap.js and bootstrap.min.js). Use <script type="module"> to load Bootstrap in the browser:
Using ESM in the browser requires full paths or an import map for bare module specifiers like @floating-ui/dom and vanilla-calendar-pro. Here’s a complete example using an import map:
Components that don’t depend on Floating UI (like Toast, Alert, Collapse, etc.) can be imported individually without any import map:
Some plugins and CSS components depend on other plugins. If you include plugins individually, make sure to check for these dependencies in the docs.
Our menus, popovers, and tooltips also depend on Floating UI. Our datepicker also depends on Vanilla Calendar Pro.
Nearly all Bootstrap plugins can be enabled and configured through HTML alone with data attributes (our preferred way of using JavaScript functionality). Be sure to only use one set of data attributes on a single element (e.g., you cannot trigger a tooltip and dialog from the same button.)
As options can be passed via data attributes or JavaScript, you can append an option name to data-bs-, as in data-bs-animation="{value}". Make sure to change the case type of the option name from “camelCase” to “kebab-case” when passing the options via data attributes. For example, use data-bs-custom-class="beautifier" instead of data-bs-customClass="beautifier".
All components support a reserved data attribute data-bs-config that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}' and data-bs-title="456" attributes, the final title value will be 456 and the separate data attributes will override values given on data-bs-config. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'.
The final configuration object is the merged result of data-bs-config, data-bs-, and js object where the latest given key-value overrides the others.
We use the native querySelector and querySelectorAll methods to query DOM elements for performance reasons, so you must use valid selectors. If you use special selectors like collapse:Example, be sure to escape them.
Bootstrap provides custom events for most plugins' unique actions. Generally, these come in an infinitive and past participle form - where the infinitive (ex. show) is triggered at the start of an event, and its past participle form (ex. shown) is triggered on the completion of an action.
All infinitive events provide preventDefault() functionality. This provides the ability to stop the execution of an action before it starts. Returning false from an event handler will also automatically call preventDefault().
All constructors accept an optional options object or nothing (which initiates a plugin with its default behavior):
If you’d like to get a particular plugin instance, each plugin exposes a getInstance method. For example, to retrieve an instance directly from an element:
This method will return null if an instance is not initiated over the requested element.
Alternatively, getOrCreateInstance can be used to get the instance associated with a DOM element, or create a new one in case it wasn’t initialized.
In case an instance wasn’t initialized, it may accept and use an optional configuration object as second argument.
In addition to the getInstance and getOrCreateInstance methods, all plugin constructors can accept a DOM element or a valid CSS selector as the first argument. Plugin elements are found with the querySelector method since our plugins only support a single element.
All programmatic API methods are asynchronous and return to the caller once the transition is started, but before it ends.
Methods that show or hide a component return a promise. The promise resolves when the transition ends, so you can await the method instead of listening for an event.
These methods return a promise:
The promise resolves with no value. It resolves at the same moment the matching shown.bs.*, hidden.bs.*, or closed.bs.* event fires, so both styles run your code at the same time. Use the event when you must react to every change, including ones your own code did not start.
Because each method returns a promise, you can run steps in order without nesting listeners.
The promise also resolves when the call does nothing — for
访问原文 返回搜索 https://getbootstrap.com/docs/6.0/getting-started/javascript
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。