Javascript
Weta Bootstrap ndụ site na ngwa mgbakwunye Javascript nke arụnyere na jQuery. Mụta maka ngwa mgbakwunye ọ bụla, data anyị na nhọrọ API mmemme, yana ndị ọzọ.
Onye ma ọ bụ chịkọtara
Enwere ike itinye ngwa mgbakwunye n'otu n'otu (iji Bootstrap's onye js/dist/*.js
), ma ọ bụ ihe niile n'otu oge na-eji bootstrap.js
ma ọ bụ nke pere mpe bootstrap.min.js
(anaghị etinye ha abụọ).
Ọ bụrụ na ị na-eji ngwugwu (Webpack, Rollup…), ị nwere ike iji /js/dist/*.js
faịlụ ndị dị njikere UMD.
Ndabere
Ụfọdụ plugins na akụkụ CSS na-adabere na plugins ndị ọzọ. Ọ bụrụ na ị gụnyere plugins n'otu n'otu, jide n'aka na ịlele maka ndabere ndị a na docs. Rịba ama na plugins niile dabere na jQuery (nke a pụtara jQuery ga-etinyerịrị tupu faịlụ ngwa mgbakwunye). Gaa na anyịpackage.json
ka ịhụ ụdị jQuery akwadoro.
Ihe ndapụta anyị, popovers na ndụmọdụ ngwaọrụ dabere na Popper.js .
Njirimara data
Ihe fọrọ nke nta ka ọ bụrụ plugins Bootstrap niile nwere ike ịme ma hazie ya site na HTML naanị ya na njirimara data (ụzọ kachasị amasị anyị iji ọrụ Javascript). Jide n'aka na ị ga -eji naanị otu njirimara data na otu mmewere (dịka ọmụmaatụ, ị nweghị ike ịkpalite ngwa ọrụ na modal site na otu bọtịnụ ahụ.)
Agbanyeghị, n'ọnọdụ ụfọdụ ọ nwere ike ịdị mma iji gbanyụọ ọrụ a. Iji gbanyụọ njirimara data API, wepụ ihe omume niile dị na akwụkwọ aha ya data-api
dị ka:
$(document).off('.data-api')
N'aka nke ọzọ, iji kwado ngwa mgbakwunye a kapịrị ọnụ, tinye naanị aha ngwa mgbakwunye dị ka oghere aha yana data-api namespace dị ka nke a:
$(document).off('.alert.data-api')
Ndị na-ahọpụta
Ugbu a iji jụọ ihe DOM anyị na-eji usoro ala querySelector
yana querySelectorAll
maka ebumnuche arụmọrụ, yabụ ị ga-eji ndị na-ahọpụta ndị ziri ezi . Ọ bụrụ na ị na-eji ndị nhọpụta pụrụ iche, dịka ọmụmaatụ: collapse:Example
jide n'aka na ị ga-agbanarị ha.
Ihe omume
Bootstrap na-enye mmemme omenala maka ọtụtụ omume pụrụ iche nke plugins. N'ozuzu, ndị a na-abịa n'ụdị njedebe na nke gara aga - ebe njedebe (dịka show
) na-akpalite na mmalite nke mmemme, na ụdị nsonye ya gara aga (dịka shown
) na-akpalite na mmecha nke omume.
Ihe omume enweghị ngwụcha na-enye preventDefault()
ọrụ. Nke a na-enye ikike ịkwụsị mmezu nke ihe tupu ya amalite. Iweghachi ụgha site na onye njikwa mmemme ga-akpọkwa akpaghị aka preventDefault()
.
$('#myModal').on('show.bs.modal', function (event) {
if (!data) {
return event.preventDefault() // stops modal from being shown
}
})
API mmemme
Anyị kwenyekwara na ị ga-enwe ike iji plugins Bootstrap naanị site na API Javascript. API ọhaneze niile bụ otu, ụzọ enwere ike ịgbanye, wee weghachi mkpokọta emere.
$('.btn.danger').button('toggle').addClass('fat')
Ụzọ niile kwesịrị ịnakwere ihe nhọrọ nhọrọ, eriri nke lekwasịrị anya otu usoro, ma ọ bụ ihe ọ bụla (nke na-amalite ngwa mgbakwunye nwere omume ndabara):
$('#myModal').modal() // initialized with defaults
$('#myModal').modal({ keyboard: false }) // initialized with no keyboard
$('#myModal').modal('show') // initializes and invokes show immediately
Ngwa mgbakwunye ọ bụla na-ekpughekwa ihe nrụpụta akụrụngwa ya na akụrụngwa Constructor
: $.fn.popover.Constructor
. Ọ bụrụ na ị ga-achọ ịnweta otu ngwa mgbakwunye ihe atụ, weghachite ya ozugbo na mmewere: $('[rel="popover"]').data('popover')
.
Ọrụ asynchronous na ntụgharị
Ụzọ API mmemme niile enweghị atụ ma laghachikwuru onye na-akpọ oku ozugbo mgbanwe malitere mana tupu ọ akwụsị .
Iji mee ihe ozugbo mgbanwe ahụ gwụchara, ị nwere ike ige ntị na mmemme kwekọrọ.
$('#myCollapse').on('shown.bs.collapse', function (event) {
// Action to execute once the collapsible area is expanded
})
Na mgbakwunye, a ga-eleghara oku usoro na mpaghara mgbanwe anya .
$('#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 !!
Ntọala mbụ
Ị nwere ike ịgbanwe ntọala ndabara maka ngwa mgbakwunye site n'ịgbanwe ihe ngwa mgbakwunye Constructor.Default
:
// changes default for the modal plugin's `keyboard` option to false
$.fn.modal.Constructor.Default.keyboard = false
Enweghị esemokwu
Mgbe ụfọdụ, ọ dị mkpa iji plugins Bootstrap nwere usoro UI ndị ọzọ. N'ọnọdụ ndị a, ndakọrịta oghere aha nwere ike ime mgbe ụfọdụ. Ọ bụrụ na nke a emee, ị nwere ike ịkpọ .noConflict
na ngwa mgbakwunye ịchọrọ iweghachi uru nke.
var bootstrapButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
$.fn.bootstrapBtn = bootstrapButton // give $().bootstrapBtn the Bootstrap functionality
Nọmba ụdị
Enwere ike ịnweta ụdị nke ọ bụla nke Bootstrap's jQuery plugins site na VERSION
akụrụngwa nke ihe nrụpụta ngwa mgbakwunye. Dịka ọmụmaatụ, maka ngwa mgbakwunye ngwa ngwa:
$.fn.tooltip.Constructor.VERSION // => "4.6.2"
Enweghị ndaghachi azụ pụrụ iche mgbe Javascript nwere nkwarụ
Ihe mgbakwunye Bootstrap anaghị ada azụ n'ọmarịcha mgbe Javascript nwere nkwarụ. Ọ bụrụ na ị na-eche banyere ahụmahụ onye ọrụ na nke a, jiri <noscript>
kọwaa ọnọdụ ahụ (na otu esi eme ka Javascript dịghachi) nye ndị ọrụ gị, na / ma ọ bụ gbakwunye ọdịda omenala nke gị.
Ụlọ ọba akwụkwọ ndị ọzọ
Bootstrap anaghị akwado ọba akwụkwọ JavaScript nke ndị ọzọ dị ka Prototype ma ọ bụ jQuery UI. N'agbanyeghị .noConflict
na ihe omume kpọpụtara aha, enwere ike ịnwe nsogbu ndakọrịta nke ịchọrọ idozi n'onwe gị.
Ul
Faịlụ JavaScript nke Bootstrap niile dabere na util.js
ọ ga-etinye ya n'akụkụ faịlụ Javascript ndị ọzọ. Ọ bụrụ na ị na-eji nke achịkọtara (ma ọ bụ miniified) bootstrap.js
, ọ dịghị mkpa itinye nke a-ọ dịlarị ebe ahụ.
util.js
gụnyere ọrụ ịba uru yana onye enyemaka bụ isi maka transitionEnd
mmemme yana emulator mgbanwe CSS. Ndị plugins ndị ọzọ na-eji ya iji lelee nkwado mgbanwe CSS yana ijide mgbanwe nkwudo.
Sanitizer
Ntuziaka ngwaọrụ na Popovers na-eji sanitizer arụnyere n'ime anyị iji kpochapụ nhọrọ ndị na-anabata HTML.
whiteList
Uru ndabara bụ nke a:
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: []
}
Ọ bụrụ na ịchọrọ ịgbakwunye ụkpụrụ ọhụrụ na ndabara a whiteList
ị nwere ike ime ihe ndị a:
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)
Ọ bụrụ na ịchọrọ ịgafe sanitizer anyị n'ihi na ịchọrọ iji ọbá akwụkwọ raara onwe ya nye, dịka ọmụmaatụ DOMPurify , ị ga-eme ihe ndị a:
$('#yourTooltip').tooltip({
sanitizeFn: function (content) {
return DOMPurify.sanitize(content)
}
})