Fa'ase'e ile anotusi autu Fa'ase'e ile su'ega fa'amatalaga
Check
in English

Javascript

Fa'aola Bootstrap i la matou fa'apipi'i JavaScript. A'oa'o e uiga i mea fa'apipi'i ta'itasi, a matou fa'amaumauga ma polokalame API filifiliga, ma isi mea.

Ta'ito'atasi pe tuufaatasia

E mafai ona fa'apipi'i fa'apipi'i ta'ito'atasi (fa'aaogā le tagata ta'ito'atasi a Bootstrap js/dist/*.js), po'o fa'aaoga uma i le taimi e tasi bootstrap.jspo'o le fa'aitiitiga bootstrap.min.js(aua le aofia uma).

Afai e te faʻaogaina se fusi (Webpack, Parcel, Vite…), e mafai ona e faʻaogaina /js/dist/*.jsfaila ua saunia UMD.

Fa'aoga ma fa'ava'a JavaScript

E ui o le Bootstrap CSS e mafai ona faʻaogaina i soʻo se faʻavae, o le Bootstrap JavaScript e leʻo fetaui lelei ma JavaScript faʻavae e pei o React, Vue, ma Angular lea e faʻaalia le malamalama atoatoa o le DOM. E mafai e Bootstrap ma le auivi ona taumafai e sui le elemene DOM lava e tasi, e mafua ai faʻamaʻi e pei o le pa'ū o loʻo pipii i le tulaga "tatala".

O se isi mea e sili atu mo i latou o loʻo faʻaogaina lenei ituaiga o faʻavaʻa o le faʻaogaina lea o se pusa faʻapitoa-faʻapitoa nai lo le Bootstrap JavaScript. O nisi nei o filifiliga sili ona lauiloa:

Faʻaaogaina Bootstrap o se module

Taumafai oe lava ia! Faʻapipiʻi le code source ma le demo galue mo le faʻaaogaina o Bootstrap o se ES module mai le twbs/examples repository . E mafai foi ona e tatalaina le faʻataʻitaʻiga i StackBlitz .

Matou te tuʻuina atu se faʻataʻitaʻiga o Bootstrap fausia e pei o ESM( bootstrap.esm.jsma bootstrap.esm.min.js) lea e mafai ai ona e faʻaogaina Bootstrap o se module i le suʻesuʻega, pe a lagolagoina e au suʻesuʻega faʻatatau .

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

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

Fa'atusatusa i fa'amaufa'ailoga a le JS, o le fa'aogaina o le ESM i le su'esu'ega e mana'omia ai oe e fa'aoga le ala atoa ma le igoa faila nai lo le igoa o le module. Faitau atili e uiga i JS modules i le browser. O le mea lena matou te faʻaaogaina 'bootstrap.esm.min.js'nai lo 'bootstrap'luga. Ae ui i lea, o lenei mea e sili atu ona faigata i le tatou Popper faʻalagolago, lea e faʻaulu mai ai Popper i totonu o la tatou JavaScript pei o lea:

import * as Popper from "@popperjs/core"

Afai e te faʻataʻitaʻiina lenei mea, o le a e vaʻai i se mea sese i totonu o le faʻamafanafanaga pei o mea nei:

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

Ina ia faʻaleleia lenei mea, e mafai ona e faʻaogaina se importmape foia ai igoa faʻapitoa e faʻamaeʻa ai auala. Afai e le lagolagoina e au su'esu'ega fa'atatauimportmap , e mana'omia lou fa'aogaina o le poloketi es-module-shims . O le auala lenei e aoga mo Bootstrap ma 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>

Fa'alagolago

O nisi fa'apipi'i ma vaega CSS fa'alagolago i isi fa'apipi'i. Afai e te faʻapipiʻiina taʻitasi plugins, ia mautinoa e siaki mo nei faʻalagolago ile docs.

E fa'alagolago fo'i a matou fa'alalo, popovers, ma meafaigaluega i Popper .

Uiga fa'amaumauga

Toeitiiti lava o Bootstrap plugins e mafai ona faʻaogaina ma faʻapipiʻi e ala ile HTML naʻo faʻatasi ai ma faʻamatalaga uiga (o la tatou auala sili e faʻaogaina ai le JavaScript). Ia mautinoa e naʻo le tasi le seti o faʻamatalaga uiga i luga o se elemene e tasi (faʻataʻitaʻiga, e le mafai ona e faʻaosoina se meafaigaluega ma auala mai le ki lava e tasi.)

A'o mafai ona pasia filifiliga e ala i fa'amatalaga uiga po'o le JavaScript, e mafai ona e fa'aopoopoina se igoa filifiliga i le data-bs-, pei o le data-bs-animation="{value}". Ia mautinoa e sui le ituaiga mataupu o le igoa filifiliga mai le " CamelCase " i le " kebab-case " pe a pasia filifiliga e ala i faʻamatalaga uiga. Mo se faʻataʻitaʻiga, faʻaaoga data-bs-custom-class="beautifier"nai lo le data-bs-customClass="beautifier".

E pei o le Bootstrap 5.2.0, o vaega uma e lagolagoina se faʻataʻitaʻiga faʻaagaga faʻamaumauga uiga data-bs-confige mafai ona faʻapipiʻi faigofie vaega faʻapipiʻi o se manoa JSON. Afai ei ai se elemene data-bs-config='{"delay":0, "title":123}'ma data-bs-title="456"uiga, o le titletau mulimuli o le ai ai 456ma o faʻamatalaga faʻamatalaga uiga ese o le a faʻamalo ai tau o loʻo tuʻuina atu ile data-bs-config. E le gata i lea, o faʻamaumauga o loʻo i ai nei e mafai ona faʻapipiʻi JSON tau pei o data-bs-delay='{"show":0,"hide":150}'.

Tagata filifilia

Matou te fa'aogaina le gagana fa'ale-aganu'u querySelectorma querySelectorAllmetotia e fesiligia ai elemene DOM mo mafua'aga o fa'atinoga, o lea e tatau ai ona e fa'aoga tagata filifilia . Afai e te fa'aogaina ni filifiliga fa'apitoa e pei o collapse:Example, ia mautinoa e sola ese mai ai.

Mea na tutupu

O le Bootstrap e tuʻuina atu mea faʻapitoa mo le tele o mea faʻapitoa a plugins. E masani lava, o nei mea e sau i se fa'ailoga fa'apitoa ma le taimi ua tuana'i - lea e fa'aosoina ai le infinitive (ex. show) i le amataga o se mea na tupu, ma lona fa'ailoga participle ua mavae (ex. shown) e fa'aosoina pe a mae'a se gaioiga.

O mea fa'apitoa uma e maua ai preventDefault()galuega fa'atino. O lenei mea e maua ai le malosi e taofi ai le faʻatinoina o se gaioiga aʻo leʻi amataina. O le toe fa'afo'i sese mai se tagata e fa'atino mea na tupu e otometi fo'i ona vala'au preventDefault().

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

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

API polokalame

E talia uma e le au faufale se mea e filifili pe leai se mea (lea e amataina ai se faʻapipiʻi ma ana amioga le lelei):

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

Afai e te manaʻo e maua se faʻataʻitaʻiga faʻapitoa, e faʻaalia e faʻapipiʻi taʻitasi se getInstancemetotia. Mo se faʻataʻitaʻiga, ia toe aumai saʻo se faʻataʻitaʻiga mai se elemene:

bootstrap.Popover.getInstance(myPopoverEl)

O lenei metotia o le a toe foʻi mai nullpe a le amataina se faʻataʻitaʻiga i luga o le elemene talosaga.

I le isi itu, getOrCreateInstancee mafai ona faʻaaoga e maua ai le faʻataʻitaʻiga e fesoʻotaʻi ma se elemene DOM, pe fatuina se mea fou pe a leʻi amataina.

bootstrap.Popover.getOrCreateInstance(myPopoverEl, configObject)

Afai e le'i amataina se fa'ata'ita'iga, e mafai ona talia ma fa'aaoga se mea fa'aopoopo e fai ma finauga lona lua.

CSS filifilia i tagata faufale

I le faʻaopoopoga i le getInstancema getOrCreateInstancemetotia, e mafai e tagata faʻapipiʻi faʻapipiʻi uma ona talia se elemene DOM poʻo se tagata filifilia CSS aoga e fai ma finauga muamua. E maua elemene faʻapipiʻi i le querySelectorauala talu ai o matou plugins e naʻo le lagolagoina o se elemene e tasi.

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')

Gaioiga asynchronous ma suiga

O metotia API fa'apolokalame uma e le fa'aogaina ma toe fo'i i le tagata vala'au pe a amata le suiga, ae le'i uma . Ina ia mafai ona faʻatino se gaioiga pe a maeʻa le suiga, e mafai ona e faʻalogo i le mea na tupu.

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

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

E le gata i lea, o le a le amanaʻiaina se auala e valaʻau ai i se vaega o suiga .

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 !!

disposeauala

E ui e foliga mai e saʻo le faʻaogaina o le disposemetotia i le taimi lava e uma hide()ai, o le a taʻitaʻia ai iʻuga le saʻo. O se faʻataʻitaʻiga lea o le faʻaaogaina o faʻafitauli:

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

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

Fa'atonuga

E mafai ona e suia tulaga fa'aletonu mo se fa'apipi'i e ala i le suia o le Constructor.Defaultmea fa'apipi'i:

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

Metotia ma meatotino

Soʻo se Bootstrap plugin e faʻaalia auala nei ma mea faʻapitoa.

Metotia Fa'amatalaga
dispose Fa'aleaga le faiga o se elemene. (Ave'ese fa'amaumauga o lo'o teuina ile elemene DOM)
getInstance Metotia static lea e mafai ai ona e maua le faʻataʻitaʻiga faʻataʻitaʻiga e fesoʻotaʻi ma se elemene DOM.
getOrCreateInstance Metotia static e mafai ai ona e maua le faʻataʻitaʻiga faʻataʻitaʻiga e fesoʻotaʻi ma se elemene DOM, pe fatuina se mea fou pe a leʻi amataina.
Meatotino tumau Fa'amatalaga
NAME Toe faafoi le igoa plugin. (Fa'aa'oa'oga: bootstrap.Tooltip.NAME)
VERSION O le fa'asologa o fa'apipi'i ta'itasi a Bootstrap e mafai ona maua e ala ile VERSIONmeatotino a le faufale (Fa'aa'oa'oga: bootstrap.Tooltip.VERSION)

Sanitizer

Tooltips ma Popovers e fa'aogaina le matou fa'amama fa'afale e fa'amama ai filifiliga e talia HTML.

O le tau fa'aletonu allowListo mea nei:

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: []
}

Afai e te manaʻo e faʻaopoopo ni tau fou i lenei faaletonu allowListe mafai ona e faia mea nei:

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)

Afai e te manaʻo e faʻamalo le matou sanitizer ona e te manaʻo e faʻaoga se faletusi faʻapitoa, mo se faʻataʻitaʻiga DOMpurify , e tatau ona e faia mea nei:

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

Pe fa'aaoga le jQuery

E te le manaʻomia le jQuery i le Bootstrap 5 , ae e mafai lava ona faʻaoga a matou vaega i le jQuery. Afai e iloa e Bootstrap jQueryi totonu o le windowmea, o le a faʻaopoopoina uma a matou vaega i le polokalama faʻapipiʻi a le jQuery. O lenei mea e mafai ai ona e faia mea nei:

$('[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

E faapena foi i isi a tatou vaega.

Leai se feteenaiga

O nisi taimi e manaʻomia le faʻaogaina o Bootstrap plugins ma isi faʻavae UI. I ia tulaga, e mafai ona tupu i nisi taimi le fete'ena'iga o igoa. Afai e tupu lenei mea, e mafai ona e valaʻau .noConflicti le plugin e te manaʻo e toe faʻafoʻi le tau o.

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

E le lagolagoina aloaia e Bootstrap faletusi JavaScript isi vaega e pei o le Prototype poʻo le jQuery UI. E ui lava .noConflictma fa'aigoa mea tutupu, e ono iai fa'afitauli feso'ota'iga e mana'omia ona e fa'aleleia e oe lava.

jQuery mea tutupu

O le a iloa e Bootstrap le jQuery pe afai jQueryo loʻo i ai i totonu o le windowmea ma e leai se data-bs-no-jqueryuiga faʻatulagaina i luga ole <body>. Afai e maua le jQuery, Bootstrap o le a faʻaalia mea faʻafetai i le jQuery's event system. Afai e te manaʻo e faʻalogo i mea na tutupu a Bootstrap, e tatau ona e faʻaogaina le jQuery metotia ( .on, .one) nai lo le addEventListener.

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

Ua le atoatoa le Javascript

O plugini a Bootstrap e leai se fa'aletonu fa'apitoa pe a fa'aletonu le JavaScript. Afai e te popole e uiga i le tagata faʻaoga i lenei tulaga, faʻaaoga <noscript>e faʻamatala ai le tulaga (ma pe faʻafefea ona toe faʻaogaina le JavaScript) i au tagata faʻaoga, ma / pe faʻaopoopo au lava faʻaleaga masani.