Holo i ka ʻike nui Holo i ka hoʻokele docs
Check
in English

JavaScript

E hoʻolaʻa iā Bootstrap me kā mākou plugin JavaScript koho. E aʻo e pili ana i kēlā me kēia plugin, kā mākou ʻikepili a me nā koho API papahana, a me nā mea hou aku.

Hoʻokahi a i hui pū ʻia

Hiki ke hoʻohui pū ʻia nā plugins (me ka hoʻohana ʻana i ke kanaka ʻo Bootstrap js/dist/*.js), a i ʻole i ka manawa hoʻokahi me ka hoʻohana ʻana a i bootstrap.jsʻole ka minified bootstrap.min.js(mai hoʻokomo i nā mea ʻelua).

Inā ʻoe e hoʻohana i kahi pūpū (Webpack, Parcel, Vite…), hiki iā ʻoe ke hoʻohana i /js/dist/*.jsnā faila i mākaukau UMD.

Hoʻohana me nā kāʻei JavaScript

ʻOiai hiki ke hoʻohana ʻia ka Bootstrap CSS me kekahi ʻano hana, ʻaʻole kūpono loa ka Bootstrap JavaScript me nā framework JavaScript e like me React, Vue, a me Angular e manaʻo nei i ka ʻike piha o ka DOM. Hiki i nā Bootstrap a me ka framework ke hoʻāʻo e hoʻololi i ka mea DOM like, e hopena i nā pōpoki e like me nā hāʻule iho i paʻa i ke kūlana "open".

ʻO kahi koho maikaʻi aʻe no ka poʻe e hoʻohana ana i kēia ʻano o ka frameworks ʻo ia ka hoʻohana ʻana i kahi pūʻolo kikoʻī framework ma kahi o ka Bootstrap JavaScript. Eia kekahi o nā koho kaulana loa:

Ke hoʻohana nei iā Bootstrap ma ke ʻano he module

E ho'āʻo iā ʻoe iho! Hoʻoiho i ke kumu kumu a me ka demo hana no ka hoʻohana ʻana iā Bootstrap ma ke ʻano he module ES mai ka waihona twbs/examples . Hiki iā ʻoe ke wehe i ka laʻana ma StackBlitz .

Hāʻawi mākou i kahi mana o Bootstrap i kūkulu ʻia e like me ESM( bootstrap.esm.jsa bootstrap.esm.min.js) e hiki ai iā ʻoe ke hoʻohana iā Bootstrap ma ke ʻano he module i ka polokalamu kele pūnaewele, inā kākoʻo kāu mau polokalamu kele pūnaewele .

<script type="module">
  import { Toast } from 'bootstrap.esm.min.js'

  Array.from(document.querySelectorAll('.toast'))
    .forEach(toastNode => new Toast(toastNode))
</script>

Hoʻohālikelike ʻia me nā pūʻulu JS, me ka hoʻohana ʻana iā ESM i ka polokalamu kele pūnaewele pono ʻoe e hoʻohana i ke ala piha a me ka filename ma kahi o ka inoa module. E heluhelu hou e pili ana i nā modula JS i ka polokalamu kele pūnaewele. No laila mākou e hoʻohana ai 'bootstrap.esm.min.js'ma mua o ka mea i 'bootstrap'luna. Eia naʻe, ʻoi aku ka paʻakikī o kēia ma muli o ko mākou hilinaʻi Popper, nāna e hoʻokomo iā Popper i kā mākou JavaScript e like me:

import * as Popper from "@popperjs/core"

Inā ʻoe e hoʻāʻo i kēia, e ʻike ʻoe i kahi hewa i ka console e like me kēia:

Uncaught TypeError: Failed to resolve module specifier "@popperjs/core". Relative references must start with either "/", "./", or "../".

No ka hoʻoponopono ʻana i kēia, hiki iā ʻoe ke hoʻohana i kahi importmape hoʻoholo ai i nā inoa module arbitrary e hoʻopau i nā ala. Inā ʻaʻole kākoʻo kāu mau polokalamu kele pūnaeweleimportmap , pono ʻoe e hoʻohana i ka papahana es-module-shims . Eia pehea e hana ai no Bootstrap a me Popper:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
    <title>Hello, modularity!</title>
  </head>
  <body>
    <h1>Hello, modularity!</h1>
    <button id="popoverButton" type="button" class="btn btn-primary btn-lg" class="btn btn-lg btn-danger" data-bs-toggle="popover" title="ESM in Browser" data-bs-content="Bang!">Custom popover</button>

    <script async src="https://cdn.jsdelivr.net/npm/es-module-shims@1/dist/es-module-shims.min.js" crossorigin="anonymous"></script>
    <script type="importmap">
    {
      "imports": {
        "@popperjs/core": "https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js",
        "bootstrap": "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.esm.min.js"
      }
    }
    </script>
    <script type="module">
      import * as bootstrap from 'bootstrap'

      new bootstrap.Popover(document.getElementById('popoverButton'))
    </script>
  </body>
</html>

Nā hilinaʻi

Ke hilinaʻi nei kekahi mau plugins a me CSS i nā plugins ʻē aʻe. Inā hoʻokomo ʻoe i nā plugins i kēlā me kēia, e hōʻoia e nānā i kēia mau hilinaʻi i nā docs.

ʻO kā mākou hāʻule iho, popovers, a me nā mea hana e hilinaʻi pū ʻia ma Popper .

Nā hiʻohiʻona ʻikepili

Ma kahi kokoke i nā plugins Bootstrap hiki ke hoʻohana a hoʻonohonoho ʻia ma o HTML wale nō me nā ʻano ʻikepili (ʻo kā mākou ala makemake e hoʻohana ai i ka hana JavaScript). E hoʻohana wale i hoʻokahi pūʻulu o nā hiʻohiʻona ʻikepili ma kahi mea hoʻokahi (e laʻa, ʻaʻole hiki iā ʻoe ke hoʻāla i kahi hāmeʻa a me ka modal mai ka pihi hoʻokahi.)

Hiki ke hāʻawi ʻia nā koho ma o nā ʻikepili a i ʻole JavaScript, hiki iā ʻoe ke hoʻohui i kahi inoa koho i data-bs-, e like me ka data-bs-animation="{value}". E hōʻoia e hoʻololi i ke ʻano hihia o ka inoa koho mai " CamelCase " i " kebab-case " i ka wā e hāʻawi ai i nā koho ma o nā ʻano ʻikepili. No ka laʻana, hoʻohana data-bs-custom-class="beautifier"ma kahi o data-bs-customClass="beautifier".

E like me Bootstrap 5.2.0, kākoʻo nā ʻāpana āpau i kahi hiʻohiʻona ʻikepili i mālama ʻiadata-bs-config i hiki ke mālama i ka hoʻonohonoho ʻāpana maʻalahi ma ke ʻano he kaula JSON. Ke loaʻa i kahi mea data-bs-config='{"delay":0, "title":123}'a me data-bs-title="456"nā ʻano, ʻo ka titlewaiwai hope loa 456a ʻo nā ʻano ʻikepili ʻokoʻa e kāpae i nā waiwai i hāʻawi ʻia ma data-bs-config. Eia hou, hiki i nā ʻikepili ʻikepili ke hoʻopaʻa i nā waiwai JSON e like me data-bs-delay='{"show":0,"hide":150}'.

Nā mea koho

Hoʻohana mākou i ka ʻōiwi querySelectora me querySelectorAllnā ala e nīnau ai i nā mea DOM no nā kumu hana, no laila pono ʻoe e hoʻohana i nā mea koho kūpono . Inā hoʻohana ʻoe i nā mea koho kūikawā e like me collapse:Example, e ʻike pono e pakele iā lākou.

Nā hanana

Hāʻawi ʻo Bootstrap i nā hanana maʻamau no ka hapa nui o nā hana kūʻokoʻa o nā plugins. ʻO ka mea maʻamau, hele mai kēia i loko o kahi ʻano infinitive a me nā mea i hala - kahi i hoʻomaka ai ka infinitive (ex. show) i ka hoʻomaka ʻana o kahi hanana, a ua hoʻāla ʻia kona ʻano participle i hala (ex. shown) i ka pau ʻana o kahi hana.

Hāʻawi nā hanana infinitive āpau i preventDefault()ka hana. Hāʻawi kēia i ka hiki ke hoʻopau i ka hoʻokō ʻana i kahi hana ma mua o ka hoʻomaka ʻana. ʻO ka hoʻihoʻi ʻana i ka wahaheʻe mai kahi mea hoʻokele hanana e kāhea ʻakomi ʻia preventDefault().

const myModal = document.querySelector('#myModal')

myModal.addEventListener('show.bs.modal', event => {
  if (!data) {
    return event.preventDefault() // stops modal from being shown
  }
})

API papahana

ʻAe nā mea kūkulu hale i kahi mea koho koho a ʻaʻohe mea (e hoʻomaka i kahi plugin me kāna ʻano paʻamau):

const myModalEl = document.querySelector('#myModal')

const modal = new bootstrap.Modal(myModalEl) // initialized with defaults

const configObject = { keyboard: false }
const modal1 = new bootstrap.Modal(myModalEl, configObject) // initialized with no keyboard

Inā makemake ʻoe e kiʻi i kahi hiʻohiʻona plugin, e hōʻike ana kēlā me kēia plugin i kahi getInstanceala. No ka laʻana, e kiʻi pololei i kahi laʻana mai kahi mea:

bootstrap.Popover.getInstance(myPopoverEl)

E hoʻi kēia ʻano hana nullinā ʻaʻole i hoʻomaka ʻia kahi laʻana ma luna o ka mea i noi ʻia.

ʻO kahi ʻē aʻe, getOrCreateInstancehiki ke hoʻohana ʻia no ka loaʻa ʻana o ka mea pili me kahi mea DOM, a i ʻole e hana i kahi mea hou inā ʻaʻole i hoʻomaka ʻia.

bootstrap.Popover.getOrCreateInstance(myPopoverEl, configObject)

Inā ʻaʻole i hoʻomaka ʻia kahi laʻana, hiki iā ia ke ʻae a hoʻohana i kahi mea hoʻonohonoho koho ma ke ʻano he pane ʻelua.

Nā mea koho CSS i nā mea hana

Ma waho aʻe o nā ʻano getInstancea me getOrCreateInstancenā ʻano, hiki i nā mea hana plugin ke ʻae i kahi mea DOM a i ʻole kahi koho CSS kūpono e like me ka hoʻopaʻapaʻa mua. Loaʻa nā mea plugin me ke querySelectorʻano no ka mea ke kākoʻo wale nei kā mākou plugins i kahi mea hoʻokahi.

const modal = new bootstrap.Modal('#myModal')
const dropdown = new bootstrap.Dropdown('[data-bs-toggle="dropdown"]')
const offcanvas = bootstrap.Offcanvas.getInstance('#myOffcanvas')
const alert = bootstrap.Alert.getOrCreateInstance('#myAlert')

Nā hana asynchronous a me nā hoʻololi

ʻAʻole like nā ʻano API papahana a pau a hoʻi i ka mea kelepona ke hoʻomaka ka hoʻololi, akā ma mua o ka pau ʻana . I mea e hoʻokō ai i kahi hana ke pau ka hoʻololi ʻana, hiki iā ʻoe ke hoʻolohe i ka hanana pili.

const myCollapseEl = document.querySelector('#myCollapse')

myCollapseEl.addEventListener('shown.bs.collapse', event => {
  // Action to execute once the collapsible area is expanded
})

Eia kekahi, e nānā ʻole ʻia kahi ala e kāhea ai i kahi mea hoʻololi .

const myCarouselEl = document.querySelector('#myCarousel')
const carousel = bootstrap.Carousel.getInstance(myCarouselEl) // Retrieve a Carousel instance

myCarouselEl.addEventListener('slid.bs.carousel', event => {
  carousel.to('2') // Will slide to the slide 2 as soon as the transition to slide 1 is finished
})

carousel.to('1') // Will start sliding to the slide 1 and returns to the caller
carousel.to('2') // !! Will be ignored, as the transition to the slide 1 is not finished !!

disposeʻano hana

ʻOiai he pololei paha ka hoʻohana ʻana i ke disposeʻano ma hope koke o hide()ka , e alakaʻi i nā hopena hewa. Eia kekahi laʻana o ka hoʻohana pilikia:

const myModal = document.querySelector('#myModal')
myModal.hide() // it is asynchronous

myModal.addEventListener('shown.bs.hidden', event => {
  myModal.dispose()
})

Nā hoʻonohonoho paʻamau

Hiki iā ʻoe ke hoʻololi i nā hoʻonohonoho paʻamau no kahi plugin ma ka hoʻololi ʻana i ka mea o ka plugin Constructor.Default:

// changes default for the modal plugin's `keyboard` option to false
bootstrap.Modal.Default.keyboard = false

Nā ʻano a me nā waiwai

Hōʻike kēlā me kēia Bootstrap plugin i nā ʻano hana a me nā waiwai static.

Kaʻina hana wehewehe
dispose Hoʻopau i ke ʻano o kekahi mea. (Wehe i ka ʻikepili i mālama ʻia ma ka mea DOM)
getInstance ʻO ke ala static e hiki ai iā ʻoe ke kiʻi i ka laʻana modal e pili ana me kahi mea DOM.
getOrCreateInstance ʻO ke ala static e hiki ai iā ʻoe ke kiʻi i ka laʻana modal e pili ana i kahi mea DOM, a i ʻole hana i kahi mea hou inā ʻaʻole i hoʻomaka ʻia.
Waiwai kūmau wehewehe
NAME Hoʻihoʻi i ka inoa plugin. (Laʻana: bootstrap.Tooltip.NAME)
VERSION Hiki ke kiʻi ʻia ka mana o kēlā me kēia o nā plugins Bootstrap ma o ka VERSIONwaiwai o ka mea hana o ka plugin (Laʻana: bootstrap.Tooltip.VERSION)

Mea hoʻomaʻemaʻe

Hoʻohana ʻo Tooltips a me Popovers i kā mākou sanitizer i kūkulu ʻia e hoʻomaʻemaʻe i nā koho e ʻae iā HTML.

ʻO kēia ka waiwai paʻamau allowList:

const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
const DefaultAllowlist = {
  // Global attributes allowed on any supplied element below.
  '*': ['class', 'dir', 'id', 'lang', 'role', ARIA_ATTRIBUTE_PATTERN],
  a: ['target', 'href', 'title', 'rel'],
  area: [],
  b: [],
  br: [],
  col: [],
  code: [],
  div: [],
  em: [],
  hr: [],
  h1: [],
  h2: [],
  h3: [],
  h4: [],
  h5: [],
  h6: [],
  i: [],
  img: ['src', 'srcset', 'alt', 'title', 'width', 'height'],
  li: [],
  ol: [],
  p: [],
  pre: [],
  s: [],
  small: [],
  span: [],
  sub: [],
  sup: [],
  strong: [],
  u: [],
  ul: []
}

Inā makemake ʻoe e hoʻohui i nā waiwai hou i kēia paʻamau allowList, hiki iā ʻoe ke hana i kēia:

const myDefaultAllowList = bootstrap.Tooltip.Default.allowList

// To allow table elements
myDefaultAllowList.table = []

// To allow td elements and data-bs-option attributes on td elements
myDefaultAllowList.td = ['data-bs-option']

// You can push your custom regex to validate your attributes.
// Be careful about your regular expressions being too lax
const myCustomRegex = /^data-my-app-[\w-]+/
myDefaultAllowList['*'].push(myCustomRegex)

Inā makemake ʻoe e kāpae i kā mākou sanitizer no ka mea makemake ʻoe e hoʻohana i kahi waihona i hoʻolaʻa ʻia, no ka laʻana DOMPurify , pono ʻoe e hana i kēia:

const yourTooltipEl = document.querySelector('#yourTooltip')
const tooltip = new bootstrap.Tooltip(yourTooltipEl, {
  sanitizeFn(content) {
    return DOMPurify.sanitize(content)
  }
})

Ke hoʻohana nei i ka jQuery

ʻAʻole pono ʻoe i ka jQuery ma Bootstrap 5 , akā hiki nō ke hoʻohana i kā mākou ʻāpana me jQuery. Inā ʻike ʻo Bootstrap i jQueryka windowmea, e hoʻohui ʻo ia i kā mākou ʻāpana āpau i ka ʻōnaehana plugin o jQuery. Hiki iā ʻoe ke hana i kēia:

$('[data-bs-toggle="tooltip"]').tooltip() // to enable tooltips, with default configuration

$('[data-bs-toggle="tooltip"]').tooltip({ boundary: 'clippingParents', customClass: 'myClass' }) // to initialize tooltips with given configuration

$('#myTooltip').tooltip('show') // to trigger `show` method

Pela no na mea e ae.

ʻAʻohe paio

I kekahi manawa pono e hoʻohana i nā plugins Bootstrap me nā ʻōnaehana UI ʻē aʻe. I kēia mau kūlana, hiki i nā hui hoʻopaʻa inoa ke hiki i kekahi manawa. Inā hiki kēia, hiki iā ʻoe ke kelepona .noConflicti ka plugin āu e makemake ai e hoʻihoʻi i ka waiwai o.

const bootstrapButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
$.fn.bootstrapBtn = bootstrapButton // give $().bootstrapBtn the Bootstrap functionality

ʻAʻole kākoʻo ʻo Bootstrap i nā waihona JavaScript ʻaoʻao ʻekolu e like me Prototype a i ʻole jQuery UI. ʻOiai .noConflicta me nā hanana i hoʻopaʻa inoa ʻia, aia paha nā pilikia kūpono e pono ai ʻoe e hoʻoponopono iā ʻoe iho.

jQuery hanana

E ʻike ʻo Bootstrap i ka jQuery inā jQueryaia i loko o ka windowmea a ʻaʻohe mea data-bs-no-jqueryi kau ʻia ma <body>. Inā loaʻa ʻo jQuery, e hoʻopuka ʻo Bootstrap i nā hanana mahalo i ka ʻōnaehana hanana o jQuery. No laila inā makemake ʻoe e hoʻolohe i nā hanana o Bootstrap, pono ʻoe e hoʻohana i nā ala jQuery ( .on, .one) ma kahi o addEventListener.

$('#myTab a').on('shown.bs.tab', () => {
  // do something...
})

Ua pio ka JavaScript

ʻAʻohe hāʻule kūikawā ko Bootstrap plugins ke pio ʻo JavaScript. Inā makemake ʻoe i ka ʻike mea hoʻohana i kēia hihia, e hoʻohana <noscript>e wehewehe i ke kūlana (a pehea e hoʻā hou ai i ka JavaScript) i kāu mea hoʻohana, a/a i ʻole e hoʻohui i kāu mau hāʻule maʻamau.