JavaScript
Nqa Bootstrap rau lub neej nrog peb xaiv JavaScript plugins ua rau jQuery. Kawm paub txog txhua lub plugin, peb cov ntaub ntawv thiab cov kev xaiv API programmatic, thiab lwm yam.
Tus kheej los yog muab tso ua ke
Plugins tuaj yeem suav nrog tus kheej (siv Bootstrap tus neeg js/dist/*.js
), lossis tag nrho ib zaug siv bootstrap.js
lossis cov minified bootstrap.min.js
(tsis suav ob qho tib si).
Yog tias koj siv lub pob (Webpack, Rollup…), koj tuaj yeem siv /js/dist/*.js
cov ntaub ntawv uas yog UMD npaj.
Kev vam meej
Qee cov plugins thiab CSS cov khoom nyob ntawm lwm cov plugins. Yog tias koj suav nrog plugins ib tus zuj zus, nco ntsoov xyuas cov kev vam meej hauv cov ntaub ntawv. Tsis tas li ntawd nco ntsoov tias tag nrho cov plugins nyob ntawm jQuery (qhov no txhais tau tias jQuery yuav tsum tau suav nrog ua ntej cov ntaub ntawv plugin). Nrog pebpackage.json
saib seb jQuery versions twg tau txais kev txhawb nqa.
Peb cov dropdowns, popovers thiab tooltips kuj nyob ntawm Popper.js .
Cov ntaub ntawv yam ntxwv
Yuav luag txhua Bootstrap plugins tuaj yeem qhib thiab teeb tsa los ntawm HTML ib leeg nrog cov ntaub ntawv cwj pwm (peb txoj kev nyiam siv JavaScript ua haujlwm). Nco ntsoov tsuas yog siv ib txheej ntawm cov ntaub ntawv tus cwj pwm ntawm ib lub caij (xws li, koj tsis tuaj yeem ua rau cov lus qhia thiab cov qauv ntawm tib lub pob.)
Txawm li cas los xij, hauv qee qhov xwm txheej nws yuav xav tau lov tes taw qhov kev ua haujlwm no. Txhawm rau lov tes taw cov ntaub ntawv tus cwj pwm API, unbind tag nrho cov xwm txheej ntawm daim ntawv teev npe nrog data-api
xws li:
$(document).off('.data-api')
Xwb, txhawm rau tsom ib lub plugin tshwj xeeb, tsuas yog suav nrog lub plugin lub npe ua lub npe chaw nrog rau cov ntaub ntawv-api namespace zoo li no:
$(document).off('.alert.data-api')
Cov xaiv
Tam sim no los nug DOM cov ntsiab lus peb siv cov txheej txheem ib txwm ua querySelector
thiab querySelectorAll
rau kev ua haujlwm vim li cas, yog li koj yuav tsum siv cov neeg xaiv siv tau . Yog tias koj siv cov xaiv tshwj xeeb, piv txwv li: collapse:Example
nco ntsoov khiav lawv.
Cov xwm txheej
Bootstrap muab cov xwm txheej kev cai rau feem ntau plugins 'kev ua tshwj xeeb. Feem ntau, cov no tuaj nyob rau hauv ib daim ntawv infinitive thiab yav dhau los koom nrog - qhov twg qhov infinitive (ex. show
) yog tshwm sim thaum pib ntawm ib qho kev tshwm sim, thiab nws yav dhau los kev koom tes (ex. shown
) yog tshwm sim ntawm qhov ua tiav ntawm qhov kev txiav txim.
Tag nrho cov txheej xwm infinitive muab preventDefault()
functionality. Qhov no muab lub peev xwm los nres qhov kev txiav txim ua ntej nws pib. Rov qab tsis muaj tseeb los ntawm tus neeg saib xyuas kev tshwm sim kuj tseem yuav hu xov tooj preventDefault()
.
$('#myModal').on('show.bs.modal', function (event) {
if (!data) {
return event.preventDefault() // stops modal from being shown
}
})
Programmatic API
Peb kuj ntseeg tias koj yuav tsum tuaj yeem siv tag nrho Bootstrap plugins nkaus xwb los ntawm JavaScript API. Tag nrho cov pej xeem APIs yog ib leeg, chainable txoj kev, thiab xa rov qab cov sau ua raws li.
$('.btn.danger').button('toggle').addClass('fat')
Txhua txoj hauv kev yuav tsum lees txais qhov kev xaiv xaiv khoom, ib txoj hlua uas tsom rau ib txoj hauv kev, lossis tsis muaj dab tsi (uas pib lub plugin nrog tus cwj pwm tsis zoo):
$('#myModal').modal() // initialized with defaults
$('#myModal').modal({ keyboard: false }) // initialized with no keyboard
$('#myModal').modal('show') // initializes and invokes show immediately
Txhua lub plugin kuj nthuav tawm nws cov neeg tsim khoom nyoos ntawm cov Constructor
cuab yeej: $.fn.popover.Constructor
. Yog tias koj xav tau ib qho plugin tshwj xeeb, muab nws ncaj qha los ntawm lub caij: $('[rel="popover"]').data('popover')
.
Asynchronous muaj nuj nqi thiab kev hloov pauv
Txhua txoj kev API programmatic yog asynchronous thiab rov qab mus rau tus neeg hu thaum qhov kev hloov pauv pib tab sis ua ntej nws xaus .
Txhawm rau ua qhov kev txiav txim thaum qhov kev hloov pauv tiav, koj tuaj yeem mloog cov xwm txheej sib thooj.
$('#myCollapse').on('shown.bs.collapse', function (event) {
// Action to execute once the collapsible area is expanded
})
Tsis tas li ntawd, ib txoj kev hu rau ib qho kev hloov pauv yuav raug ignored .
$('#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 !!
Default settings
Koj tuaj yeem hloov qhov chaw pib rau lub plugin los ntawm kev hloov kho lub plugin cov Constructor.Default
khoom:
// changes default for the modal plugin's `keyboard` option to false
$.fn.modal.Constructor.Default.keyboard = false
Tsis muaj teeb meem
Qee lub sij hawm nws yog qhov tsim nyog los siv Bootstrap plugins nrog rau lwm qhov UI lub luag haujlwm. Hauv cov xwm txheej no, kev sib tsoo ntawm lub npe chaw tuaj yeem tshwm sim qee zaus. Yog tias qhov no tshwm sim, koj tuaj yeem hu .noConflict
rau lub plugin koj xav rov qab tus nqi ntawm.
var bootstrapButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
$.fn.bootstrapBtn = bootstrapButton // give $().bootstrapBtn the Bootstrap functionality
Cov lej
Cov version ntawm txhua qhov ntawm Bootstrap's jQuery plugins tuaj yeem nkag tau los ntawm cov VERSION
cuab yeej ntawm plugin tus tsim. Piv txwv li, rau tooltip plugin:
$.fn.tooltip.Constructor.VERSION // => "4.6.2"
Tsis muaj qhov cuam tshuam tshwj xeeb thaum JavaScript raug kaw
Bootstrap's plugins tsis poob rov qab tshwj xeeb zoo nkauj thaum JavaScript raug kaw. Yog tias koj mob siab txog cov neeg siv kev paub dhau los hauv qhov no, siv <noscript>
los piav qhia qhov xwm txheej (thiab yuav ua li cas rov qhib JavaScript) rau koj cov neeg siv, thiab / lossis ntxiv koj tus kheej cov kev cai poob qis.
Cov tsev qiv ntawv thib peb
Bootstrap tsis ua haujlwm txhawb nqa JavaScript cov tsev qiv ntawv thib peb xws li Prototype lossis jQuery UI. Txawm hais tias .noConflict
thiab cov xwm txheej muaj npe, tej zaum yuav muaj teeb meem kev sib raug zoo uas koj yuav tsum tau kho ntawm koj tus kheej.
Siv
Txhua Bootstrap's JavaScript cov ntaub ntawv nyob ntawm util.js
thiab nws yuav tsum tau suav nrog rau lwm cov ntaub ntawv JavaScript. Yog tias koj siv cov ntawv sau ua ke (lossis me me) bootstrap.js
, tsis tas yuav suav nrog qhov no - nws twb muaj lawm.
util.js
suav nrog cov khoom siv hluav taws xob thiab tus pab cuam yooj yim rau transitionEnd
cov xwm txheej nrog rau CSS kev hloov pauv emulator. Nws tau siv los ntawm lwm cov plugins los kuaj xyuas CSS kev hloov pauv kev txhawb nqa thiab txhawm rau ntes dai hloov pauv.
Tshuaj ntxuav tes
Cov lus qhia thiab Popovers siv peb cov tshuaj ntxuav tes ua ke los ntxuav cov kev xaiv uas lees txais HTML.
Tus whiteList
nqi pib yog cov hauv qab no:
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: []
}
Yog tias koj xav ntxiv qhov tseem ceeb tshiab rau lub neej ntawd whiteList
koj tuaj yeem ua cov hauv qab no:
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)
Yog tias koj xav hla peb cov tshuaj tua kab mob vim tias koj nyiam siv lub tsev qiv ntawv tshwj xeeb, piv txwv li DOMPurify , koj yuav tsum ua cov hauv qab no:
$('#yourTooltip').tooltip({
sanitizeFn: function (content) {
return DOMPurify.sanitize(content)
}
})