Tīpoka ki te ihirangi matua Tīpoka ki te whakaterenga tuhinga
in English

JavaScript

Whakaorangia a Bootstrap me a maatau taputapu JavaScript. Akohia mo ia mono, o maatau raraunga me nga whiringa API papatono, me etahi atu.

Takitahi, whakahiato ranei

Ka taea te whakauru takitahi nga monomai (ma te whakamahi i te takitahi a Bootstrap js/dist/*.js), i te wa kotahi ma te whakamahi bootstrap.js, te iti ranei bootstrap.min.js(kaua e whakaurua e rua).

Mena kei te whakamahi koe i te paihere (Webpack, Rollup…), ka taea e koe te whakamahi i /js/dist/*.jsnga konae kua rite te UMD.

Te whakamahi i te Bootstrap hei kōwae

Ka whakaratohia e matou he putanga o Bootstrap i hangaia hei ESM( bootstrap.esm.jsme bootstrap.esm.min.js) e taea ai e koe te whakamahi i a Bootstrap hei kōwae i roto i to tirotiro, mena ka tautokohia e to kaitirotiro kua tohua .

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

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

Moni hotokore

Na te herenga o te kaitirotiro, kaore e taea te whakamahi i etahi o a matou monomai, ko Dropdown, Tooltip me Popover mono ki te <script>tohu me te modulemomo na te mea kei te whakawhirinaki ki a Popper. Mo etahi atu korero mo te take tirohia konei .

Nga whakawhirinakitanga

Ko etahi mono me nga waahanga CSS e whakawhirinaki ana ki etahi atu mono. Mena kei te whakauru takitahi koe i nga monomai, tirohia te tirotiro mo enei whakawhirinaki i roto i nga tuhinga.

Ka whakawhirinaki ano a maatau whakahekenga, pouvers me nga taputapu taputapu ki te Popper .

Kei te hiahia tonu ki te whakamahi jQuery? Ka taea!

I hangaia a Bootstrap 5 kia whakamahia me te kore jQuery, engari ka taea tonu te whakamahi i o maatau waahanga me te jQuery. Mena ka kitea e Bootstrap jQueryi roto i te windowahanoa ka taapiri katoa o maatau waahanga ki te punaha mono a jQuery; te tikanga ka taea e koe te mahi $('[data-bs-toggle="tooltip"]').tooltip()ki te whakahohe i nga aki taputapu. He pera ano mo etahi atu waahanga.

Nga huanga raraunga

Tata ki te katoa o nga taputapu Bootstrap ka taea te whakahohe me te whirihora ma te HTML anake me nga huanga raraunga (ko ta matou huarahi pai ki te whakamahi i te mahi JavaScript). Kia mau ki te whakamahi i tetahi huinga huanga raraunga i runga i te huānga kotahi (hei tauira, kaore e taea e koe te whakaoho i te aki taputapu me te aratau mai i te paatene kotahi.)

Kaiwhiriwhiri

I tenei wa ki te uiui i nga huānga DOM ka whakamahia e matou nga tikanga taketake querySelectorme querySelectorAllnga take mahi, na me whakamahi koe i nga kaiwhiri whaimana . Mena ka whakamahi koe i nga kaiwhiriwhiri motuhake, hei tauira: collapse:Examplekia mawhiti koe.

Nga huihuinga

Ka whakarato a Bootstrap i nga huihuinga ritenga mo te nuinga o nga mahi ahurei. Ko te tikanga, ka puta mai enei i roto i te ahua whai mutunga me te ahua o mua - kei reira te infinitive (ex. show) ka puta i te timatanga o te takahanga, a ko tona puka participle o mua (ex. shown) ka puta i te otinga o te mahi.

Ko nga huihuinga mutunga kore e whakarato preventDefault()taumahinga. Ka whai waahi tenei ki te aukati i te mahi i mua i te tiimata. Ko te whakahoki teka mai i te kaikawe takahanga ka waea aunoa preventDefault().

var myModal = document.getElementById('myModal')

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

nga kaupapa jQuery

Ka kitea e Bootstrap te jQuery mena jQuerykei roto i te windowahanoa karekau he data-bs-no-jqueryhuanga kua tohua ki runga <body>. Mena ka kitea te jQuery, ka tukuna e Bootstrap nga kaupapa na te punaha takahanga a jQuery. Na, ki te hiahia koe ki te whakarongo ki nga kaupapa a Bootstrap, me whakamahi koe i nga tikanga jQuery ( .on, .one) hei utu mo addEventListener.

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

API Papatono

Ka whakaaehia e nga kaihanga katoa he ahanoa hei kowhiringa, kaore ranei (e timata ana i te mono me tana whanonga taunoa):

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

Mena kei te pirangi koe ki te tiki i tetahi tauira mono, ka whakaatuhia e ia mono he getInstancetikanga. Hei tiki tika mai i tetahi huānga, mahia tenei: bootstrap.Popover.getInstance(myPopoverEl).

Kaihoko CSS i roto i nga kaihanga

Ka taea hoki e koe te whakamahi i te kaiwhiri CSS hei tohenga tuatahi hei utu mo te huānga DOM hei arawhiti i te mono. I tenei wa ko te huānga mo te mono ka kitea e te querySelectortikanga na te mea ka tautokohia e a maatau mono tetahi huānga kotahi anake.

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

Nga mahi tukutahi me nga whakawhitinga

Ko nga tikanga API hotaka katoa he tukutahi ka hoki ki te kaiwaea ina timata te whakawhiti engari i mua i te mutunga .

Hei mahi i tetahi mahi ina oti te whakawhitinga, ka taea e koe te whakarongo ki te kaupapa e pa ana.

var myCollapseEl = document.getElementById('myCollapse')

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

I tua atu ka waihohia te waea tikanga ki tetahi waahanga whakawhiti .

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

Tautuhinga taunoa

Ka taea e koe te huri i nga tautuhinga taunoa mo te mono ma te whakarereke i te Constructor.Defaultahanoa o te mono:

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

Karekau he papā (mehemea ka whakamahi koe i te jQuery)

I etahi wa ka tika te whakamahi i nga taputapu Bootstrap me etahi atu angamahi UI. I roto i enei ahuatanga, ka taea e nga tukinga mokowā ingoa i etahi wa. Mena ka pa mai tenei, ka waea atu koe .noConflictki te mono e hiahia ana koe ki te whakahoki i te uara o.

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

Tau putanga

Ko te putanga o ia monomai a Bootstrap ka taea te uru ma te VERSIONtaonga o te kaihanga o te mono. Hei tauira, mo te taputapu taputapu:

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

Karekau he takahuri motuhake ina monokia a JavaScript

Ko nga taputapu a Bootstrap e kore e hoki whakamuri i te wa e monoa ana a JavaScript. Mena kei te whakaaro koe ki te wheako o te kaiwhakamahi i tenei keehi, whakamahia <noscript>ki te whakamarama i te ahuatanga (me pehea te whakahohe ano i te JavaScript) ki o kaiwhakamahi, me te taapiri i a koe ake ritenga takahuri.

Nga whare pukapuka tuatoru

Kaore a Bootstrap e tautoko mana i nga whare pukapuka JavaScript tuatoru penei i te Prototype, jQuery UI ranei. Ahakoa .noConflictnga huihuinga me nga ingoa mokowhiti, tera pea he raruraru hototahi hei whakatika maau ake.

Kaihoroi

Ko nga Aki Utauta me nga Popovers e whakamahi ana i ta maatau kai horoi ki te horoi i nga whiringa ka whakaae ki te HTML.

allowListKo te uara taunoa e whai ake nei:

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

Mena kei te hiahia koe ki te taapiri i nga uara hou ki tenei taunoa allowListka taea e koe te mahi:

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)

Mena kei te pirangi koe ki te karo i ta maatau kai horoi na te mea he pai ki a koe ki te whakamahi i te whare pukapuka whakatapua, hei tauira DOMPuify , me mahi e koe enei e whai ake nei:

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