Source

JavaScript

E hoʻolaʻa iā Bootstrap me kā mākou plugin JavaScript koho i kūkulu ʻia ma jQuery. 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 ʻia nā plugins (me ka hoʻohana ʻana i nā *.jsfaila pilikino a Bootstrap), a i ʻole nā ​​​​mea hoʻokahi me ka hoʻohana ʻana a i bootstrap.jsʻole ka minified bootstrap.min.js(ʻaʻole e hoʻokomo i nā mea ʻelua).

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. E hoʻomaopopo hoʻi e pili ana nā plugins āpau i ka jQuery (ʻo ia hoʻi, pono e hoʻokomo ʻia ʻo jQuery ma mua o nā faila plugin). E kūkākūkā i kā mākoupackage.json e ʻike i nā mana o jQuery i kākoʻo ʻia.

ʻO kā mākou hāʻule iho, popovers a me nā mea hana e hilinaʻi pū ʻia ma Popper.js .

Nā hiʻohiʻona ʻikepili

Ma kahi kokoke i nā plugins Bootstrap hiki ke hoʻohana a hoʻonohonoho ʻia ma o HTML wale nō me nā ʻano ʻ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.)

Eia naʻe, i kekahi mau kūlana, makemake paha e hoʻopau i kēia hana. No ka hoʻopau ʻana i ka API o ka ʻikepili, wehe i nā hanana āpau ma ka papa inoa i hoʻopaʻa ʻia data-apime kēia:

$(document).off('.data-api')

ʻO kahi ʻē aʻe, no ka huli ʻana i kahi plugin kikoʻī, e hoʻopili wale i ka inoa o ka plugin ma ke ʻano he inoa inoa me ka inoa data-api e like me kēia:

$(document).off('.alert.data-api')

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().

$('#myModal').on('show.bs.modal', function (e) {
  if (!data) return e.preventDefault() // stops modal from being shown
})

API papahana

Manaʻo mākou e hiki iā ʻoe ke hoʻohana i nā plugins Bootstrap āpau ma o ka JavaScript API. ʻO nā API lehulehu āpau he ʻano hoʻokahi, hiki ke hoʻopaʻa ʻia, a hoʻihoʻi i ka hōʻiliʻili i hana ʻia.

$('.btn.danger').button('toggle').addClass('fat')

Pono nā ala āpau e ʻae i kahi mea koho koho, kahi kaula e kuhikuhi ana i kahi ala kūikawā, a i ʻole ʻole (e hoʻomaka ana i kahi plugin me ka hana paʻamau):

$('#myModal').modal()                      // initialized with defaults
$('#myModal').modal({ keyboard: false })   // initialized with no keyboard
$('#myModal').modal('show')                // initializes and invokes show immediately

Hōʻike ʻia kēlā me kēia plugin i kāna mea kūkulu maka ma kahi Constructorwaiwai: $.fn.popover.Constructor. Inā makemake ʻoe e kiʻi i kahi laʻana plugin, e kiʻi pololei iā ia mai kahi mea: $('[rel="popover"]').data('popover').

Nā hana asynchronous a me nā hoʻololi

ʻAʻole like nā kaʻina 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.

$('#myCollapse').on('shown.bs.collapse', function (e) {
  // Action to execute once the collapsible area is expanded
})

Eia kekahi , e nānā ʻole ʻia ke kāhea ʻana i kahi ʻāpana hoʻololi .

$('#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 !!

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:

$.fn.modal.Constructor.Default.keyboard = false // changes default for the modal plugin's `keyboard` option to false

ʻAʻohe paio

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 .noConflicti 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 jQuery Bootstrap ma o ka VERSIONwaiwai o ka mea nāna i kūkulu. No ka laʻana, no ka plugin tooltip:

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

ʻ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 .noConflicta 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.

Util

Aia nā faila JavaScript a Bootstrap util.jsa pono e hoʻokomo pū me nā faila JavaScript ʻē aʻe. Inā ʻoe e hoʻohana nei i ka compiled (a i ʻole miniified) bootstrap.js, ʻaʻohe pono e hoʻokomo i kēia - aia ma laila.

util.jsAia nā hana pono a me kahi mea kōkua kumu no transitionEndnā hanana a me kahi emulator hoʻololi CSS. Hoʻohana ʻia e nā plugins ʻē aʻe e nānā i ke kākoʻo hoʻololi CSS a e hopu i nā hoʻololi e kau ana.