in English

JavaScript

Whakaorangia a Bootstrap me a maatau taputapu JavaScript kua hangaia ki runga jQuery. Akohia mo ia mono, o maatau raraunga me nga whiringa API papatono, me etahi atu.

Takitahi, whakahiato ranei

Ka taea te whakauru takitahi nga monomai (ma te whakamahi i te tangata takitahi a Bootstrap js/dist/*.js), me te katoa i te wa kotahi ma te whakamahi bootstrap.js, te iti ranei bootstrap.min.js(kaua e whakaurua e rua).

Mena kei te whakamahi koe i te paihere (Webpack, Rollup…), ka taea e koe te whakamahi i /js/dist/*.jsnga konae kua reri UMD.

Nga whakawhirinakitanga

Ko etahi mono me nga waahanga CSS e whakawhirinaki ana ki etahi atu mono. Mena kei te whakauru takitahi koe i nga monomai, tirohia enei whakawhirinakitanga kei roto i nga tuhinga. Kia mahara ano ko nga monomai katoa e whakawhirinaki ana ki te jQuery (ko te tikanga me whakauru te jQuery i mua i nga konae mono). Korerohia mai ki a maataupackage.json ki te kite he aha nga momo putanga o te jQuery e tautokohia ana.

Ka whakawhirinaki ano a maatau whakahekenga, pouvers me nga taputapu taputapu ki Popper.js .

Nga huanga raraunga

Tata ki te katoa o nga taputapu Bootstrap ka taea te whakahohe me te whirihora ma te HTML anake me nga huanga raraunga (ko ta matou huarahi pai ki te whakamahi i te mahi JavaScript). Kia mau ki te whakamahi i tetahi huinga huanga raraunga i runga i te huānga kotahi (hei tauira, kaore e taea e koe te whakaoho i te aki taputapu me te aratau mai i te paatene kotahi.)

Heoi, i etahi ahuatanga ka hiahia pea ki te whakakore i tenei mahi. Hei mono i te API huanga raraunga, wetewetehia nga takahanga katoa i runga i te mokowāingoa tuhinga me te data-apipenei:

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

Hei taapiri, ki te aro ki tetahi mono motuhake, whakauruhia te ingoa o te mono hei mokowāingoa me te mokowāingoa raraunga-api penei:

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

Kaiwhiriwhiri

I tenei wa ki te uiui i nga huānga DOM ka whakamahia e matou nga tikanga taketake querySelectorme querySelectorAllnga take mahi, na me whakamahi koe i nga kaiwhiri whaimana . Mena ka whakamahi koe i nga kaiwhiriwhiri motuhake, hei tauira: collapse:Examplekia mawhiti koe.

Nga huihuinga

Ka whakarato a Bootstrap i nga huihuinga ritenga mo te nuinga o nga mahi ahurei. Ko te tikanga, ka puta mai enei i roto i te ahua whai mutunga me te ahua o mua - kei reira te infinitive (ex. show) ka puta i te timatanga o te takahanga, a ko tona puka participle o mua (ex. shown) ka puta i te otinga o te mahi.

Ko nga huihuinga mutunga kore e whakarato preventDefault()taumahinga. Ka whai waahi tenei ki te aukati i te mahi i mua i te tiimata. Ko te whakahoki teka mai i te kaikawe takahanga ka waea aunoa preventDefault().

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

API Papatono

E whakapono ana matou ka taea e koe te whakamahi i nga taputapu Bootstrap katoa ma te JavaScript API. Ko nga API whanui katoa he tikanga kotahi, ka taea te mekameka, ka whakahoki i te kohinga i mahia.

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

Ko nga tikanga katoa me whakaae ki tetahi ahanoa kōwhiringa, he aho e aro ana ki tetahi tikanga, kaore ranei (e timata ana i te mono me te whanonga taunoa):

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

Ka whakaatu hoki ia mono i tana kaihanga mata ki runga i tetahi Constructorrawa: $.fn.popover.Constructor. Mena kei te pirangi koe ki te tiki i tetahi tauira mono, tiki tika mai i tetahi huānga: $('[rel="popover"]').data('popover').

Nga mahi tukutahi me nga whakawhitinga

Ko nga tikanga API hotaka katoa he tukutahi ka hoki ki te kaiwaea ina timata te whakawhiti engari i mua i te mutunga .

Hei mahi i tetahi mahi ina oti te whakawhitinga, ka taea e koe te whakarongo ki te kaupapa e pa ana.

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

I tua atu ka waihohia te waea tikanga ki tetahi waahanga whakawhiti .

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

Tautuhinga taunoa

Ka taea e koe te huri i nga tautuhinga taunoa mo te mono ma te whakarereke i te Constructor.Defaultahanoa o te mono:

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

Karekau he pakanga

I etahi wa ka tika te whakamahi i nga taputapu Bootstrap me etahi atu angamahi UI. I roto i enei ahuatanga, ka taea e nga tukinga mokowā ingoa i etahi wa. Mena ka pa mai tenei, ka waea atu koe .noConflictki te mono e hiahia ana koe ki te whakahoki i te uara o.

var bootstrapButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
$.fn.bootstrapBtn = bootstrapButton // give $().bootstrapBtn the Bootstrap functionality

Tau putanga

Ko te putanga o ia o nga taputapu jQuery a Bootstrap ka taea te uru atu ma te VERSIONtaonga o te kaihanga o te mono. Hei tauira, mo te taputapu taputapu:

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

Karekau he takahuri motuhake ina monokia a JavaScript

Ko nga taputapu a Bootstrap e kore e hoki whakamuri i te wa e monoa ana a JavaScript. Mena kei te whakaaro koe ki te wheako o te kaiwhakamahi i tenei keehi, whakamahia <noscript>ki te whakamarama i te ahuatanga (me pehea te whakahohe ano i te JavaScript) ki o kaiwhakamahi, me te taapiri i a koe ake ritenga takahuri.

Nga whare pukapuka tuatoru

Kaore a Bootstrap e tautoko mana i nga whare pukapuka JavaScript tuatoru penei i te Prototype, jQuery UI ranei. Ahakoa .noConflictnga huihuinga me te mokowhiti ingoa, tera pea he raruraru hototahi hei whakatika maau ake.

Util

Ko nga konae JavaScript katoa a Bootstrap e whakawhirinaki ana util.jsme whakauru ki te taha o etahi atu konae JavaScript. Mēnā kei te whakamahi koe i te whakahiato (he iti ranei) bootstrap.js, kaore he take ki te whakauru i tenei—kei kona ano.

util.jskei roto nga mahi whaipainga me tetahi kaiawhina taketake mo transitionEndnga huihuinga me te emulator whakawhiti CSS. Ka whakamahia e etahi atu mono ki te tirotiro mo te tautoko whakawhiti CSS me te hopu i nga whakawhitinga iri.

Kaihoroi

Ko nga Aki Utauta me nga Popovers e whakamahi ana i ta maatau kai horoi i roto ki te horoi i nga whiringa ka whakaae ki te HTML.

whiteListKo te uara taunoa e whai ake nei:

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

Mena kei te hiahia koe ki te taapiri i nga uara hou ki tenei taunoa whiteListka taea e koe te mahi:

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)

Mena kei te pirangi koe ki te karo i ta maatau kai horoi na te mea he pai ki a koe te whakamahi i tetahi whare pukapuka whakatapua, hei tauira DOMPuify , me mahi e koe enei e whai ake nei:

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