in English

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 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/*.jsnā faila i mākaukau UMD.

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ā mea koho

I kēia manawa no ka nīnau ʻana i nā mea DOM hoʻohana mākou i nā ʻano maoli querySelectora querySelectorAllno 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:Examplee 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().

$('#myModal').on('show.bs.modal', function (event) {
  if (!data) {
    return event.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ā ʻ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.

$('#myCollapse').on('shown.bs.collapse', function (event) {
  // 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 (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 !!

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
$.fn.modal.Constructor.Default.keyboard = 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.6.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 .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.

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 whiteList:

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: []
}

Inā makemake ʻoe e hoʻohui i nā waiwai hou i kēia paʻamau whiteList, hiki iā ʻoe ke hana i kēia:

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)

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:

$('#yourTooltip').tooltip({
  sanitizeFn: function (content) {
    return DOMPurify.sanitize(content)
  }
})