in English

JavaScript ye

Aw ye Bootstrap lase ɲɛnamaya ma ni an ka JavaScript sɛgɛsɛgɛlikɛlanw ye minnu bɛ se ka sugandi minnu jɔlen bɛ jQuery kan. Aw ye kunnafoni sɔrɔ plugin kelen-kelen bɛɛ kan, an ka data ani porogaramu API suganditaw kan, ani fɛn wɛrɛw.

Mɔgɔ kelen-kelen walima a lajɛlen

Plugins bɛ se ka don u kelen-kelen na (ka baara kɛ ni Bootstrap ka kelen-kelen ye js/dist/*.js), walima u bɛɛ bɛ se ka don a kɔnɔ siɲɛ kelen ka baara kɛ ni bootstrap.jswalima ka dɔgɔya bootstrap.min.js(i kana u fila bɛɛ don a kɔnɔ).

N’i ​​bɛ baara Kɛ ni bundler (Webpack, Rollup...) ye, i bɛ Se ka baara Kɛ ni /js/dist/*.jsfilew ye minnu labɛnnen dòn UMD fɛ.

Dependencies (Jɛkuluw).

Plugin dɔw ni CSS yɔrɔ dɔw bɛ bɔ plugins wɛrɛw de la. N’i ​​ye plugins (dakunw) Dòn u kelen-kelen na, i k’a lajɛ k’o dependencies (daɲɛw) ninnu Lajɛ docs (dɔgɔkun) kɔnɔ. Aw k’a kɔlɔsi fana ko plugins bɛɛ bɛ tali kɛ jQuery de la (o kɔrɔ ye ko jQuery ka kan ka don a kɔnɔ ka kɔn plugin files ɲɛ). Aw ye anw lajɛpackage.json walasa k’a dɔn jQuery bɔko minnu bɛ dɛmɛ.

An ka dropdowns, popovers ani baarakɛminɛnw fana bɛ bɔ Popper.js de la.

Donanw ka fɛnw

Bootstrap 'kɔnɔfɛnw bɛɛ bɛ Se ka Dabɔ ani k'u Labɛn HTML dɔrɔn fɛ ni data attributes (an ka JavaScript baarakɛcogo ɲuman ye). Aw ye aw jija ka baara kɛ ni kunnafoni-falen-falen-yɔrɔ kelen dɔrɔn ye fɛn kelen kan (misali la, aw tɛ se ka baarakɛminɛnw ni modɛli dɔ daminɛ ka bɔ butɔni kelen na.)

Nka, ko dɔw la, a bɛ se ka kɛ ko a ka ɲi ka nin baarakɛcogo in bali. Walasa ka data attribut API bali, i ka ko kɛlenw bɛɛ bɔ sɛbɛn tɔgɔla yɔrɔ la ni data-apii n’a fɔ nin cogo la:

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

O cogo kelen na, walasa ka fɛn kɛrɛnkɛrɛnnen dɔ Laɲini, i bɛ fɛn in tɔgɔ Dòn dɔrɔn i n’a fɔ tɔgɔda-yɔrɔ ka Fàra data-api tɔgɔda-yɔrɔ kan i n’a fɔ nin:

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

Sugandilikɛlaw

Sisan walasa ka DOM fɛnw ɲininka an bɛ baara Kɛ ni native fɛɛrɛw ye querySelectorani querySelectorAllbaarakɛcogo kunw na, o la i ka kan ka baara Kɛ ni sugandilikɛlaw ye minnu bɛ Se ka Kɛ . Ni aw bɛ baara kɛ ni sugandili kɛrɛnkɛrɛnnenw ye, misali la: collapse:Exampleaw ye aw jija ka boli u ɲɛ.

Ko minnu kɛra

Bootstrap bɛ ko kɛlenw di minnu bɛ kɛ ka kɛɲɛ ni mɔgɔw sago ye, ka ɲɛsin plugins fanba ka wale kɛrɛnkɛrɛnnenw ma. A ka c’a la, ninnu bɛ Nà daɲɛ-dafalen ni daɲɛ tɛmɛnen cogo la - daɲɛ-dafalen (ex. show) bɛ Daminɛ yɔrɔ min na ko dɔ daminɛ na, wa a daɲɛ tɛmɛnen cogoya (ex. shown) bɛ Daminɛ wale dɔ dafalen na.

Ko minnu tɛ dantigɛ, olu bɛɛ bɛ preventDefault()baarakɛcogo Di. O bɛ se di ka wale dɔ waleyali jɔ sani a ka daminɛ. nkalon seginni ka bɔ ko kɛlenw ɲɛnabɔbaga dɔ la , o fana bɛna wele a yɛrɛma preventDefault().

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

API porogaramulen

An dalen b’a la fana ko i ka kan ka se ka baara kɛ ni Bootstrap plugins bɛɛ ye JavaScript API dɔrɔn fɛ. Foroba APIw bɛɛ ye fɛɛrɛ kelenw ye, minnu bɛ se ka siri ni cakɛda ye, ani ka segin dalajɛlen kan min waleyara.

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

Fɛɛrɛ bɛɛ ka kan ka sɔn sugandili fɛn dɔ ma, sɛrɛ min bɛ fɛɛrɛ kɛrɛnkɛrɛnnen dɔ laɲini, walima foyi tɛ (min bɛ fɛn dɔ daminɛ ni kɛcogo kɔrɔ ye):

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

Plugin kelen-kelen bɛɛ fana b’a ka raw constructor jira Constructornafolo dɔ kan: $.fn.popover.Constructor. N’i ​​b’a fɛ ka plugin instance kɛrɛnkɛrɛnnen dɔ sɔrɔ, i k’a sɔrɔ k’a ɲɛsin element dɔ ma: $('[rel="popover"]').data('popover').

Baarakɛcogo minnu tɛ kelen ye ani fɛn caman tigɛli

porogaramu API fɛɛrɛw bɛɛ ye asynchrone ye wa u bɛ segin welebaga ma ni wuli daminɛna nka sanni a ka ban .

Walasa ka wale dɔ Kɛ ni 'tɛmɛsira Banna, i bɛ Se ka ko kɛlen min bɛ Bɛn o ma, i bɛ Se k'o Lamɛ.

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

ka fara o kan , fɛɛrɛ weleli dɔ bɛna kɛ fɛn ye min bɛ wuli ka bɔ a nɔ na .

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

Settings (Labɛnw) minnu bɛ daminɛ

Aw bɛ se ka fɛn dɔ labɛncogo kɔrɔw Changer ni aw ye fɛn dɔ sɛmɛntiya Constructor.Default:

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

Kɛlɛ si tɛ yen

Tuma dɔw la a ka kan ka baara kɛ ni Bootstrap plugins ye ni UI framework wɛrɛw ye. O cogoyaw la, tɔgɔda-yɔrɔ-ko bɛ Se ka Kɛ tuma dɔw la. N'o Kɛra, i bɛ Se ka weleli .noConflictKɛ i b'a fɛ ka min nafa Lasegin.

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

Version nimɔrɔw

Bootstrap ka jQuery 'sènfɛ-sɛbɛn kelen-kelen bɛɛ 'sèn bɛ Se ka Sɔrɔ 'sènfɛ-sɛbɛnni -minɛn VERSIONin dilannikɛla ka nafolo fɛ. Misali la, baarakɛminɛnw ɲɛfɔli dakun na:

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

Fallback kɛrɛnkɛrɛnnen si tɛ yen ni JavaScript ma baara kɛ

Bootstrap ka plugins tɛ bin kɔfɛ kɛrɛnkɛrɛnnenya la gracefully ni JavaScript bɛ baara la. N’i ​​b’i janto baarakɛlaw ka ko kɛlenw na nin ko in na, i ka baara kɛ ni <noscript>u ye walasa ka ko in ɲɛfɔ (ani JavaScript daminɛcogo kura) i ka baarakɛlaw ye, ani/walima ka i yɛrɛ ka ladamu fallbacks fara a kan.

Mɔgɔ sabananw ka gafemarayɔrɔw

Bootstrap tɛ JavaScript gafemarayɔrɔ wɛrɛw dɛmɛ foroba la i n’a fɔ Prototype walima jQuery UI. Hali .noConflictni ni tɔgɔda-ko kɛlenw bɛ yen, bɛnkan gɛlɛyaw bɛ se ka sɔrɔ minnu ka kan ka ɲɛnabɔ i yɛrɛ ma.

Util

Bootstrap ka JavaScript filew bɛɛ bɛ Bɔ a la util.jswa a ka kan ka Dòn JavaScript file tɔw kɛrɛfɛ. N’i ​​bɛ baara Kɛ ni compiled (walima minified) bootstrap.jsye , kun t’a la k’o Dòn a la—a bɛ yen kaban.

util.jsnafalan baarakɛcogo ani dɛmɛn jɔnjɔn dɔ bɛ sɔrɔ transitionEndko kɛlenw na ka fara CSS jiginni misali kan. A bɛ Kɛ ni 'plugin tɔw ye walasa ka CSS 'tɛmɛsira dɛmɛni Lajɛ ani ka 'tɛmɛsira jɛgɛlenw Minɛ.

Sanitizer ye

Baarakɛminɛnw ni Popovers bɛ baara Kɛ ni an ka saniyalan jɔlen ye walasa ka sugandiliw saniya minnu bɛ Sɔn HTML ma.

A whiteListnafa fɔlɔ ye nin ye:

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

N’i ​​b’a fɛ ka nafa kuraw Fàra nin default in kan whiteListi bɛ Se ka nin wale ninnu Kɛ:

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)

N’aw b’a fɛ ka tɛmɛ an ka saniyalan kan bawo aw b’a fɛ ka baara kɛ ni gafemarayɔrɔ kɛrɛnkɛrɛnnen ye, misali la DOMPurify , aw ka kan ka nin wale ninnu kɛ:

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