JavaSkript fɔ di Baybul
Bring Bootstrap to layf wit wi opshɔnal JavaSkript plɔgin dɛn we dɛn bil pan jQuery. Lan bɔt ɛni plɔgin, wi data ɛn programmatik API opshɔn dɛn, ɛn ɔda tin dɛn.
Wan wan ɔ we dɛn dɔn kɔmpilayt
Plɔgin dɛn kin inklud wan wan (yuz Bootstrap in wan wan js/dist/*.js
), ɔ ɔl wan tɛm yuz bootstrap.js
ɔ di minified bootstrap.min.js
(nɔ inklud ɔl tu).
If yu yuz bundler (Webpack, Rollup...), yu kin yuz /js/dist/*.js
fayl dɛn we dɔn rɛdi fɔ UMD.
Dipɛnsin dɛn
Sɔm plɔgin dɛn ɛn CSS kɔmpɔnɛnt dɛn de dipen pan ɔda plɔgin dɛn. If yu put plɔgin dɛn wan bay wan, mek shɔ se yu chɛk fɔ dɛn dipɛnsin dɛn ya na di dɔkyumɛnt dɛn. Notis bak se ɔl di plɔgin dɛn dipen pan jQuery (dis min se jQuery fɔ de bifo di plɔgin fayl dɛn). Kɔnsul wipackage.json
fɔ si us vɛshɔn dɛn fɔ jQuery dɛn de sɔpɔt.
Wi dropdowns, popovers ɛn tultips dɛn bak de dipen pan Popper.js .
Data atribyut dɛn
Klose to ɔl di Bootstrap plɔgin dɛn kin ɛnabul ɛn kɔnfigyut tru HTML nɔmɔ wit data atribyut dɛn (wi we wi lɛk fɔ yuz JavaSkript funkshɔnaliti). Mek shɔ se yu jɔs yuz wan sɛt fɔ data atribyut dɛn pan wan ɛlimɛnt (ɛgz., yu nɔ go ebul fɔ trig wan tultip ɛn modal frɔm di sem bɔtin.)
Bɔt, insay sɔm tin dɛn, i kin fayn fɔ mek dɛn disable dis wok. Fɔ disable di data atribyut API, unbind ɔl di ivintɛns dɛn na di dɔkyumɛnt nemspɛs wit data-api
lɛk so:
$(document).off('.data-api')
Ɔda we de fɔ du dat, fɔ tɔch wan patikyula plɔgin, jɔs put di plɔgin in nem as nemspɛs wit di data-api nemspɛs lɛk dis:
$(document).off('.alert.data-api')
Di wan dɛn we de pik di tin dɛn
Naw fɔ aks DOM ɛlimɛnt dɛn wi de yuz di nativ we dɛn querySelector
ɛn querySelectorAll
fɔ pefɔmɛns rizin dɛn, so yu fɔ yuz valid sɛlɛktɔ dɛn . If yu yuz spɛshal sɛlɛktɔ dɛn, fɔ ɛgzampul: collapse:Example
mek shɔ se yu rɔnawe pan dɛn.
Di tin dɛn we kin apin
Bootstrap de gi kɔstɔm ivin fɔ bɔku pan di plɔgin dɛn yunik akshɔn dɛn. Jɛnɛral wan, dɛn wan ya kin kam insay wan infinitiv ɛn past patisipul fɔm - usay di infinitiv (ɛks. show
) kin trig we wan ivin bigin, ɛn in past patisipul fɔm (ɛks. shown
) kin trig we wan akshɔn dɔn.
Ɔl di infinitiv ivin dɛn de gi preventDefault()
funkshɔnaliti. Dis de gi di abiliti fɔ stɔp di ɛgzikishɔn fɔ wan akshɔn bifo i bigin. Ritɔn false frɔm wan ivent handla go kɔl bak ɔtomɛtik wan preventDefault()
.
$('#myModal').on('show.bs.modal', function (event) {
if (!data) {
return event.preventDefault() // stops modal from being shown
}
})
Programmetik API fɔ di wan dɛn we de wok
Wi biliv bak se yu fɔ ebul fɔ yuz ɔl di Bootstrap plɔgin dɛn jɔs tru di JavaSkript API. Ɔl di pɔblik API dɛn na wan, chenj we dɛn kin yuz fɔ du tin, ɛn dɛn kin gi bak di kɔleshiɔn we dɛn akt pan.
$('.btn.danger').button('toggle').addClass('fat')
Ɔl di we dɛn fɔ aksept wan opshɔnal opshɔn ɔbjɛkt, wan string we de tɔch wan patikyula we, ɔ natin (we de bigin wan plɔgin wit difɔlt bihayvya):
$('#myModal').modal() // initialized with defaults
$('#myModal').modal({ keyboard: false }) // initialized with no keyboard
$('#myModal').modal('show') // initializes and invokes show immediately
Ɛni plɔgin de ɛksplɔz in raw kɔnstrɔkta bak pan wan Constructor
prɔpati: $.fn.popover.Constructor
. If yu want fɔ gɛt wan patikyula plɔgin instans, tek am dairekt frɔm wan ɛlimɛnt: $('[rel="popover"]').data('popover')
.
Asynchronous fכnshכn dεm εn transishכn dεm
Ɔl di programmatik API mɛtɔd dɛn na asynchronous ɛn dɛn kin kam bak to di pɔsin we kɔl wans di transishɔn dɔn stat bɔt bifo i dɔn .
Fɔ mek yu ebul fɔ ɛksɛkutiv wan akshɔn wans di chenj dɔn, yu kin lisin to di kɔrɛspɔndɛns ivin.
$('#myCollapse').on('shown.bs.collapse', function (event) {
// Action to execute once the collapsible area is expanded
})
Apat frɔm dat wan mɛtɔd kɔl pan wan transishɔn kɔmpɔnɛnt go ignore .
$('#myCarousel').on('slid.bs.carousel', function (event) {
$('#myCarousel').carousel('2') // Will slide to the slide 2 as soon as the transition to slide 1 is finished
})
$('#myCarousel').carousel('1') // Will start sliding to the slide 1 and returns to the caller
$('#myCarousel').carousel('2') // !! Will be ignored, as the transition to the slide 1 is not finished !!
Difɔlt sɛtin dɛn
Yu kin chenj di difɔlt sɛtin fɔ wan plɔgin bay we yu chenj di tin we di plɔgin de Constructor.Default
yuz:
// changes default for the modal plugin's `keyboard` option to false
$.fn.modal.Constructor.Default.keyboard = false
Nɔ kɔnflikt nɔ de
Sɔntɛnde i nid fɔ yuz Bootstrap plɔgin dɛn wit ɔda UI fremwɔk dɛn. Insay dɛn kayn tin ya, nemspɛs kɔlishin kin apin wan wan tɛm. If dis apin, yu kin kɔl .noConflict
di plɔgin we yu want fɔ gi bak di valyu fɔ.
var bootstrapButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
$.fn.bootstrapBtn = bootstrapButton // give $().bootstrapBtn the Bootstrap functionality
Vɛshɔn nɔmba dɛn
Di vɛshɔn fɔ ɛni wan pan Bootstrap in jQuery plɔgin dɛn kin akses tru di VERSION
prɔpati fɔ di plɔgin in kɔnstrɔkta. Fɔ ɛgzampul, fɔ di tultip plɔgin:
$.fn.tooltip.Constructor.VERSION // => "4.6.2"
Nɔ spɛshal fɔl bak we dɛn nɔ yuz JavaSkript
Bootstrap in plɔgin dɛn nɔ de fɔdɔm bak patikyula grasful wan we JavaSkript nɔ de wok. If yu bisin bɔt di yuza ɛkspiriɛns na dis kes, yuz <noscript>
fɔ ɛksplen di sityueshɔn (ɛn aw fɔ ri-ɛnabul JavaSkript) to yu yuza dɛn, ɛn/ɔ ad yu yon kɔstɔm fɔlbak dɛn.
Tɔd-pati laybri dɛn
Bootstrap nɔ ɔfishal wan sɔpɔt tɔd-pati JavaSkript laybri dɛn lɛk Prototype ɔ jQuery UI. Pan ɔl .noConflict
we tin dɛn kin apin ɛn we gɛt nem-spɛs, prɔblɛm dɛn kin de we gɛt fɔ du wit di kɔmpitishɔn we yu nid fɔ fiks fɔ yusɛf.
Yutil
Ɔl di Bootstrap in JavaSkript fayl dɛn dipen pan util.js
ɛn i gɛt fɔ de nia di ɔda JavaSkript fayl dɛn. If yu de yuz di kɔmpilayt (ɔ minified) bootstrap.js
, nid nɔ de fɔ put dis insay—i dɔn ɔlrɛdi de.
util.js
inklud yutiliti fɛnshɔn dɛn ɛn wan besik ɛp fɔ transitionEnd
ivintɛns dɛn ɛn bak wan CSS transishɔn ɛmulatɔ. Na di ɔda plɔgin dɛn de yuz am fɔ chɛk fɔ CSS transishɔn sɔpɔt ɛn fɔ kech transishɔn dɛn we de hang.
Sanitiza we dɛn kin yuz fɔ was
Tultip ɛn Popovers de yuz wi bilt-in sanitiza fɔ sanitayz opshɔn dɛn we de aksept HTML.
Di difɔlt whiteList
valyu na di wan we de dɔŋ ya:
var ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
var DefaultWhitelist = {
// 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: []
}
If yu want fɔ ad nyu valyu dɛn to dis difɔlt whiteList
yu kin du dɛn tin ya:
var myDefaultWhiteList = $.fn.tooltip.Constructor.Default.whiteList
// To allow table elements
myDefaultWhiteList.table = []
// To allow td elements and data-option attributes on td elements
myDefaultWhiteList.td = ['data-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-]+/
myDefaultWhiteList['*'].push(myCustomRegex)
If yu want fɔ baypas wi sanitiza bikɔs yu lɛk fɔ yuz laybri we dɛn dɔn gi yu, fɔ ɛgzampul DOMPurify , yu fɔ du dɛn tin ya:
$('#yourTooltip').tooltip({
sanitizeFn: function (content) {
return DOMPurify.sanitize(content)
}
})