Source

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.jslossis 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/*.jscov 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-apixws 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 querySelectorthiab querySelectorAllrau 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:Examplenco 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 (e) {
  if (!data) return e.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 Constructorcuab 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 (e) {
  // 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 (e) {
  $('#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.Defaultkhoom:

$.fn.modal.Constructor.Default.keyboard = false // changes default for the modal plugin's `keyboard` option to 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 .noConflictrau 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 VERSIONcuab yeej ntawm plugin tus tsim. Piv txwv li, rau tooltip plugin:

$.fn.tooltip.Constructor.VERSION // => "4.2.1"

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 .noConflictthiab 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.jsthiab 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.jssuav nrog cov khoom siv hluav taws xob thiab tus pab cuam yooj yim rau transitionEndcov 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.