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ā hoʻohana ʻoe i kahi pūpū (Webpack, Rollup…), hiki iā ʻoe ke hoʻohana i /js/dist/*.js
nā faila i mākaukau UMD.
Ke hoʻohana nei iā Bootstrap ma ke ʻano he module
Hāʻawi mākou i kahi mana o Bootstrap i kūkulu ʻia e like me ESM
( bootstrap.esm.js
a bootstrap.esm.min.js
) e hiki ai iā ʻoe ke hoʻohana iā Bootstrap ma ke ʻano he module i kāu 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>
Pākuʻi like ʻole
Ma muli o nā palena o ka polokalamu kele pūnaewele, ʻaʻole hiki ke hoʻohana ʻia kekahi o kā mākou plugins, ʻo ia hoʻi ʻo Dropdown, Tooltip a me Popover plugins i kahi <script>
hōʻailona me ke module
ʻano no ka mea ua hilinaʻi lākou iā Popper. No ka 'ike hou aku e pili ana i ka pilikia e ike maanei .
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 .
Makemake ʻoe e hoʻohana i ka jQuery? Hiki nō!
Hoʻolālā ʻia ʻo Bootstrap 5 e hoʻohana ʻole me ka jQuery, akā hiki nō ke hoʻohana i kā mākou ʻāpana me jQuery. Inā ʻike ʻo Bootstrap i jQuery
ka window
mea , e hoʻohui ʻo ia i kā mākou ʻāpana āpau i ka ʻōnaehana plugin o jQuery; 'o ia ho'i, hiki iā 'oe ke hana $('[data-bs-toggle="tooltip"]').tooltip()
e hiki ai i nā mea hana. Pela no na mea e ae.
Nā hiʻohiʻona ʻikepili
Ma kahi kokoke i nā plugins Bootstrap hiki ke hoʻohana ʻia a hoʻonohonoho ʻia ma o HTML wale nō me nā hiʻohiʻona ʻ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.)
Nā mea koho
I kēia manawa no ka nīnau ʻana i nā mea DOM hoʻohana mākou i nā ʻano maoli querySelector
a querySelectorAll
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ā, no ka laʻana: collapse:Example
e hōʻoia 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()
.
var myModal = document.getElementById('myModal')
myModal.addEventListener('show.bs.modal', function (event) {
if (!data) {
return event.preventDefault() // stops modal from being shown
}
})
jQuery hanana
E ʻike ʻo Bootstrap i ka jQuery inā jQuery
aia i loko o ka window
mea a ʻaʻohe mea data-bs-no-jquery
i 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', function () {
// do something...
})
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):
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
Inā makemake ʻoe e kiʻi i kahi hiʻohiʻona plugin, e hōʻike ana kēlā me kēia plugin i kahi getInstance
ala. I mea e kiʻi pololei mai i kahi mea, e hana i kēia: bootstrap.Popover.getInstance(myPopoverEl)
.
Nā mea koho CSS i nā mea hana
Hiki iā ʻoe ke hoʻohana i kahi mea koho CSS ma ke ʻano he hoʻopaʻapaʻa mua ma mua o kahi mea DOM e hoʻomaka i ka plugin. I kēia manawa ʻike ʻia ka mea no ka plugin ma ke querySelector
ʻano no ka mea kākoʻo kā mākou plugins i hoʻokahi mea wale nō.
var modal = new bootstrap.Modal('#myModal')
var dropdown = new bootstrap.Dropdown('[data-bs-toggle="dropdown"]')
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.
var myCollapseEl = document.getElementById('myCollapse')
myCollapseEl.addEventListener('shown.bs.collapse', function (event) {
// Action to execute once the collapsible area is expanded
})
Eia kekahi , e nānā ʻole ʻia ke kelepona ʻana i kahi mea hoʻololi .
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 !!
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
ʻAʻohe paio (inā ʻoe e hoʻohana i ka jQuery)
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 .noConflict
i ka plugin āu e makemake ai e hoʻihoʻi i ka waiwai o.
var bootstrapButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
$.fn.bootstrapBtn = bootstrapButton // give $().bootstrapBtn the Bootstrap functionality
Helu helu
Hiki ke kiʻi ʻia ka mana o kēlā me kēia o nā plugins Bootstrap ma o ka VERSION
waiwai o ka mea hana o ka plugin. No ka laʻana, no ka plugin tooltip:
bootstrap.Tooltip.VERSION // => "5.0.2"
ʻAʻohe hāʻule kūikawā inā pio ka JavaScript
ʻAʻole hāʻule maikaʻi nā plugins Bootstrap i ka wā e pio ai ka 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.
Nā waihona puke ʻaoʻao ʻekolu
ʻAʻole kākoʻo ʻo Bootstrap i nā waihona JavaScript ʻaoʻao ʻekolu e like me Prototype a i ʻole jQuery UI. ʻOiai .noConflict
a 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.
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
:
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: []
}
Inā makemake ʻoe e hoʻohui i nā waiwai hou i kēia paʻamau allowList
, hiki iā ʻoe ke hana i kēia:
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)
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:
var yourTooltipEl = document.getElementById('yourTooltip')
var tooltip = new bootstrap.Tooltip(yourTooltipEl, {
sanitizeFn: function (content) {
return DOMPurify.sanitize(content)
}
})