Mafere na isi ọdịnaya Gaa na ntugharị docs
in English

Javascript

Weta Bootstrap ndụ site na mgbakwunye Javascript nhọrọ anyị. Mụta maka ngwa mgbakwunye ọ bụla, data anyị na nhọrọ API mmemme, yana ndị ọzọ.

Onye ma ọ bụ chịkọtara

Enwere ike itinye ngwa mgbakwunye n'otu n'otu (iji Bootstrap's onye js/dist/*.js), ma ọ bụ ihe niile n'otu oge na-eji bootstrap.jsma ọ bụ nke pere mpe bootstrap.min.js(anaghị etinye ha abụọ).

Ọ bụrụ na ị na-eji ngwugwu (Webpack, Rollup…), ị nwere ike iji /js/dist/*.jsfaịlụ ndị dị njikere UMD.

Iji Bootstrap dị ka modul

Anyị na-enye ụdị Bootstrap wuru dị ka ESM( bootstrap.esm.jsna bootstrap.esm.min.js) nke na-enye gị ohere iji Bootstrap dị ka modul na ihe nchọgharị gị, ma ọ bụrụ na ihe nchọgharị gị ezubere iche na-akwado ya .

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

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

Ngwa mgbakwunye na-adabaghị

N'ihi oke ihe nchọgharị, ụfọdụ plugins anyị, ya bụ Dropdown, Tooltip na Popover plugins, enweghị ike iji <script>mkpado nwere moduleụdị n'ihi na ha dabere na Popper. Maka ozi ndị ọzọ gbasara okwu ahụ, lee ebe a .

Ndabere

Ụfọdụ plugins na ngwa CSS na-adabere na plugins ndị ọzọ. Ọ bụrụ na ị gụnyere plugins n'otu n'otu, jide n'aka na ịlele maka ndabere ndị a na docs.

Ihe ndapụta anyị, popovers na ndụmọdụ ngwaọrụ dabere na Popper .

Ị ka chọrọ iji jQuery? O kwere omume!

Emebere Bootstrap 5 ka e jiri ya mee ihe na-enweghị jQuery, mana ọ ka ga-ekwe omume iji akụrụngwa anyị na jQuery. Ọ bụrụ na Bootstrap chọpụta jQueryn'ime ihe windowahụ , ọ ga-agbakwunye ihe anyị niile na ngwa ngwa jQuery; nke a pụtara na ị ga-enwe ike ime $('[data-bs-toggle="tooltip"]').tooltip()ka ndụmọdụ ngwaọrụ rụọ ọrụ. Otu ihe ahụ na-aga maka ihe ndị ọzọ anyị.

Njirimara data

Ihe fọrọ nke nta ka ọ bụrụ plugins Bootstrap niile nwere ike ịme ma hazie ya site na HTML naanị ya na njirimara data (ụzọ kachasị amasị anyị iji ọrụ Javascript). Jide n'aka na ị ga -eji naanị otu njirimara data na otu mmewere (dịka ọmụmaatụ, ị nweghị ike ịkpalite ngwa ọrụ na modal site na otu bọtịnụ ahụ.)

Ndị na-ahọpụta

Ugbu a iji jụọ ihe DOM anyị na-eji usoro ala querySelectoryana querySelectorAllmaka ebumnuche arụmọrụ, yabụ ị ga-eji ndị na-ahọpụta ndị ziri ezi . Ọ bụrụ na ị na-eji ndị nhọpụta pụrụ iche, dịka ọmụmaatụ: collapse:Examplejide n'aka na ị ga-agbanarị ha.

Ihe omume

Bootstrap na-enye mmemme omenala maka ọtụtụ omume pụrụ iche nke plugins. N'ozuzu, ndị a na-abịa n'ụdị njedebe na nke gara aga - ebe njedebe (dịka show) na-akpalite na mmalite nke mmemme, na ụdị nsonye ya gara aga (dịka shown) na-akpalite na mmecha nke omume.

Ihe omume enweghị ngwụcha na-enye preventDefault()ọrụ. Nke a na-enye ikike ịkwụsị mmezu nke ihe tupu ya amalite. Iweghachi ụgha site na onye njikwa mmemme ga-akpọkwa akpaghị aka preventDefault().

var myModal = document.getElementById('myModal')

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

mmemme jQuery

Bootstrap ga-achọpụta jQuery ma ọ bụrụ na ọ jQuerydị na windowihe ahụ ma ọ nweghị data-bs-no-jqueryàgwà atọrọ na <body>. Ọ bụrụ na achọtara jQuery, Bootstrap ga-ebupụta mmemme ekele maka sistemụ mmemme jQuery. Yabụ ọ bụrụ na ịchọrọ ige ntị na mmemme Bootstrap, ị ga-eji usoro jQuery ( .on, .one) kama addEventListener.

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

API mmemme

Ndị nrụpụta niile na-anabata ihe nhọrọ nhọrọ ma ọ bụ ihe ọ bụla (nke na-ebute ngwa mgbakwunye na omume ndabara):

var myModalEl = document.getElementById('myModal')

var modal = new bootstrap.Modal(myModalEl) // initialized with defaults
var modal = new bootstrap.Modal(myModalEl, { keyboard: false }) // initialized with no keyboard

Ọ bụrụ na ị ga-achọ ịnweta otu ngwa mgbakwunye ọmụmaatụ, ngwa mgbakwunye ọ bụla na-ekpughe otu getInstanceusoro. Ka iweghachite ya ozugbo na mmewere, mee nke a: bootstrap.Popover.getInstance(myPopoverEl).

Ndị na-ahọpụta CSS na ndị nrụpụta

Ịnwekwara ike iji onye nhọpụta CSS dị ka arụmụka mbụ kama ịbụ ihe DOM iji malite ngwa mgbakwunye ahụ. Ugbu a, a na-ahụ mmewere maka ngwa mgbakwunye site na querySelectorusoro ebe plugins anyị na-akwado naanị otu mmewere.

var modal = new bootstrap.Modal('#myModal')
var dropdown = new bootstrap.Dropdown('[data-bs-toggle="dropdown"]')

Ọrụ asynchronous na ntụgharị

Ụzọ API mmemme niile enweghị atụ ma laghachikwuru onye na-akpọ oku ozugbo mgbanwe malitere mana tupu ọ akwụsị .

Iji mee ihe ozugbo mgbanwe ahụ gwụchara, ị nwere ike ige ntị na mmemme kwekọrọ.

var myCollapseEl = document.getElementById('myCollapse')

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

Na mgbakwunye, a ga-eleghara oku usoro na mpaghara mgbanwe anya .

var myCarouselEl = document.getElementById('myCarousel')
var carousel = bootstrap.Carousel.getInstance(myCarouselEl) // Retrieve a Carousel instance

myCarouselEl.addEventListener('slid.bs.carousel', function (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 !!

Ntọala mbụ

Ị nwere ike ịgbanwe ntọala ndabara maka ngwa mgbakwunye site n'ịgbanwe ihe ngwa mgbakwunye Constructor.Default:

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

Enweghị esemokwu (naanị ma ọ bụrụ na ị na-eji jQuery)

Mgbe ụfọdụ, ọ dị mkpa iji plugins Bootstrap nwere usoro UI ndị ọzọ. N'ọnọdụ ndị a, ndakọrịta oghere aha nwere ike ime mgbe ụfọdụ. Ọ bụrụ na nke a emee, ị nwere ike ịkpọ .noConflictna ngwa mgbakwunye ịchọrọ iweghachi uru nke.

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

Nọmba ụdị

Enwere ike ịnweta ụdị plugins Bootstrap ọ bụla site na VERSIONakụrụngwa nke ihe nrụpụta ngwa mgbakwunye. Dịka ọmụmaatụ, maka ngwa mgbakwunye ngwa ngwa:

bootstrap.Tooltip.VERSION // => "5.0.2"

Enweghị ndaghachi azụ pụrụ iche mgbe Javascript nwere nkwarụ

Ihe mgbakwunye Bootstrap anaghị ada azụ n'ọmarịcha mgbe Javascript nwere nkwarụ. Ọ bụrụ na ị na-eche banyere ahụmahụ onye ọrụ na nke a, jiri <noscript>kọwaa ọnọdụ ahụ (na otu esi eme ka Javascript dịghachi) nye ndị ọrụ gị, na / ma ọ bụ gbakwunye ọdịda omenala nke gị.

Ụlọ ọba akwụkwọ ndị ọzọ

Bootstrap anaghị akwado ọba akwụkwọ JavaScript nke ndị ọzọ dị ka Prototype ma ọ bụ jQuery UI. N'agbanyeghị .noConflictna ihe omume kpọpụtara aha, enwere ike ịnwe nsogbu ndakọrịta nke ịchọrọ idozi n'onwe gị.

Sanitizer

Ntuziaka ngwaọrụ na Popovers na-eji sanitizer arụnyere n'ime anyị iji kpochapụ nhọrọ ndị na-anabata HTML.

allowListUru ndabara bụ nke a:

var ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
var 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: []
}

Ọ bụrụ na ịchọrọ ịgbakwunye ụkpụrụ ọhụrụ na ndabara a allowListị nwere ike ime ihe ndị a:

var 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
var myCustomRegex = /^data-my-app-[\w-]+/
myDefaultAllowList['*'].push(myCustomRegex)

Ọ bụrụ na ịchọrọ ịgafe sanitizer anyị n'ihi na ịchọrọ iji ọbá akwụkwọ raara onwe ya nye, dịka ọmụmaatụ DOMPurify , ị ga-eme ihe ndị a:

var yourTooltipEl = document.getElementById('yourTooltip')
var tooltip = new bootstrap.Tooltip(yourTooltipEl, {
  sanitizeFn: function (content) {
    return DOMPurify.sanitize(content)
  }
})