in English

JavaScript

Tisa Bootstrap evuton’wini hi ti-plugin ta hina ta JavaScript leti nga hlawuriwa leti akiweke eka jQuery. Dyondza hi plugin yin’wana na yin’wana, data ya hina na swihlawulekisi swa API ya phurogireme, na swin’wana.

Hi munhu hi xiyexe kumbe leyi hlengeletiweke

Ti-plugin ti nga katsiwa hi toxe (hi ku tirhisa Bootstrap’s individual js/dist/*.js), kumbe hinkwaswo hi nkarhi wun’we hi ku tirhisa bootstrap.jskumbe leswi hungutiweke bootstrap.min.js(u nga katsi havumbirhi bya swona).

Loko u tirhisa bundler (Webpack, Rollup...), u nga tirhisa /js/dist/*.jstifayela leti nga UMD ready.

Swilo leswi titshegeke hi swona

Ti-plugin tin’wana ni swiphemu swa CSS swi titshege hi ti-plugin tin’wana. Loko u katsa ti-plugin hi toxe, tiyisisa leswaku u kambela ku titshega loku eka ti-docs. Nakambe xiya leswaku ti-plugin hinkwato ti titshege hi jQuery (leswi swi vula leswaku jQuery yi fanele ku katsiwa emahlweni ka tifayela ta ti-plugin). Vulavula na hinapackage.json ku vona leswaku hi tihi tivhidiyo ta jQuery leti seketeriwaka.

Ti dropdowns ta hina, ti popovers na ti toltips na swona swi titshege hi Popper.js .

Swihlawulekisi swa datha

Kwalomu ka ti-plugin hinkwato ta Bootstrap ti nga tirhisiwa no lulamisiwa hi ku tirhisa HTML ntsena hi swihlawulekisi swa data (ndlela ya hina leyi hi yi tsakelaka yo tirhisa ntirho wa JavaScript). Tiyisisa leswaku u tirhisa ntsena sete yin’we ya swihlawulekisi swa datha eka elemente yin’we (xikombiso, a wu nge pfuli xitsundzuxo xa xitirhisiwa na modal ku suka eka buti yin’we.)

Hambiswiritano, eka swiyimo swin’wana swi nga ha tsakeriwa ku tshikisa ntirho lowu. Ku tshikisa API ya xihlawulekisi xa data, susa swiendlakalo hinkwaswo eka tsalwa ra namespaced hi data-apiku fana na sweswo:

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

Handle ka sweswo, ku kongomisa eka xiengetelo xo karhi, katsa ntsena vito ra xiengetelo tanihi ndhawu ya mavito swin’we ni ndhawu ya mavito ya data-api yo fana ni leyi:

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

Swihlawulekisi

Sweswi ku vutisa swiaki swa DOM hi tirhisa tindlela ta ntumbuluko querySelectorna querySelectorAllhi swivangelo swa matirhelo, kutani u fanele ku tirhisa swihlawulekisi leswi tirhaka . Loko u tirhisa swihlawulekisi swo hlawuleka, hi xikombiso: collapse:Exampletiyiseka leswaku u swi balekela.

Swiendlakalo

Bootstrap yi nyika swiendlakalo swa ntolovelo eka swiendlo swo hlawuleka swa ti-plugin to tala. Hi ntolovelo, leswi swi ta hi xivumbeko xa xiendliwa lexi nga heriki na xiendliwa lexi hundzeke - laha xiendliwa lexi nga heriki (ex. show) xi pfuriwaka eku sunguleni ka xiendlakalo, naswona xivumbeko xa xona xa xiendliwa lexi hundzeke (ex. shown) xi pfuriwa loko xiendlo xi hetisisiwa.

Swiendlakalo hinkwaswo leswi nga heriki swi nyika preventDefault()ntirho. Leswi swi nyika vuswikoti byo yimisa ku hetisisiwa ka xiendlo xi nga si sungula. Ku vuyisa mavunwa ku suka eka mufambisi wa xiendlakalo na swona swi ta vitana hi ku tisungulela preventDefault().

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

API ya minongonoko

Hi tlhela hi tshemba leswaku u fanele ku kota ku tirhisa ti-plugin hinkwato ta Bootstrap ntsena hi ku tirhisa JavaScript API. Ti-API hinkwato ta mani na mani i tindlela tin’we, leti nga ni tinketana, naswona ti vuyisa nhlengeleto lowu endliweke hi wona.

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

Maendlelo hinkwawo ya fanele ku amukela nchumu wa swihlawulekisi swa ku hlawula, ntambhu leyi kongomisaka eka ndlela yo karhi, kumbe ku nga ri na nchumu (leyi sungulaka xiengetelo hi mahanyelo ya xiviri):

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

Plugin yin’wana na yin’wana yi tlhela yi paluxa muaki wa yona wa raw eka Constructornhundzu: $.fn.popover.Constructor. Loko u lava ku kuma xikombiso xo karhi xa plugin, xi vuyise hi ku kongoma eka elemente: $('[rel="popover"]').data('popover').

Mintirho ya asynchronous na ku cinca

Maendlelo hinkwawo ya API ya phurogireme i ya asynchronous naswona ya tlhelela eka mufoyini loko ku cinca ku sungurile kambe ku nga si hela .

Leswaku u endla xiendlo loko ku cinca ku herile, u nga yingisela xiendlakalo lexi fambisanaka na xona.

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

Ku engetela kwalaho ku vitaniwa ka ndlela eka xiphemu lexi cincaka ku ta honisiwa .

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

Switirhisiwa swa ntolovelo

U nga cinca swiletelo swa xiviri swa xiengetelo hi ku cinca Constructor.Defaultnchumu wa xiengetelo:

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

Ku hava ku lwisana

Minkarhi yin’wana swa laveka ku tirhisa ti-plugin ta Bootstrap ni swivumbeko swin’wana swa UI. Eka swiyimo leswi, ku tlumbana ka ndhawu ya mavito ku nga humelela nkarhi wun’wana. Loko leswi swi humelela, u nga ha vitana .noConflicteka plugin leyi u lavaka ku tlherisela nkoka wa yona.

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

Tinomboro ta vuhundzuluxeri

Vuhundzuluxi bya yin’wana na yin’wana ya ti-plugin ta jQuery ta Bootstrap byi nga nghenisiwa hi ku tirhisa VERSIONnhundzu ya muaki wa xiengetelo. Xikombiso, eka xiengetelo xa xitsundzuxo xa switirhisiwa:

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

Ku hava ti fallback to hlawuleka loko JavaScript yi nga tirhi

Ti-plugin ta Bootstrap a ti tlheleli endzhaku hi ndlela yo hlawuleka loko JavaScript yi nga tirhi. Loko u khathala hi ntokoto wa mutirhisi eka mhaka leyi, tirhisa <noscript>ku hlamusela xiyimo (na ndlela yo tlhela u pfumelela JavaScript) eka vatirhisi va wena, na/kumbe ku engetela ti-fallback ta wena ta ntolovelo.

Tilayiburari ta vanhu va vunharhu

Bootstrap a yi seketeli ximfumo tilayiburari ta JavaScript ta vanhu van’wana to fana na Prototype kumbe jQuery UI. Ku nga khathariseki .noConflictswiendlakalo leswi nga ni swiphemu swa mavito, ku nga ha va ni swiphiqo swa ku fambisana leswi u faneleke u swi lulamisa hi wexe.

Util

Tifayili hinkwato ta JavaScript ta Bootstrap ti titshege hi util.jsnaswona ti fanele ku katsiwa etlhelo ka tifayela tin’wana ta JavaScript. Loko u tirhisa leyi hlengeletiweke (kumbe leyi hungutiweke) bootstrap.js, a ku na xilaveko xo katsa leswi—se swi kona.

util.jsku katsa mintirho ya vukorhokeri na mupfuni wa xisekelo wa transitionEndswiendlakalo xikan’we na xifaniso xa ku cinca ka CSS. Yi tirhisiwa hi ti-plugin tin’wana ku kambela nseketelo wa ku cinca ka CSS ni ku khoma ku cinca loku lenga-lengaka.

Xibasisi

Tooltips na Popovers swi tirhisa sanitizer ya hina leyi akiweke ku sanitize swihlawulekisi leswi amukelaka HTML.

Nhlayo ya xiviri whiteListhi leyi landzelaka:

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

Loko u lava ku engetela mimpimo leyintshwa eka default leyi whiteListu nga endla leswi landzelaka:

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)

Loko u lava ku hundza eka sanitizer ya hina hikuva u tsakela ku tirhisa layiburari leyi tinyiketeleke, xikombiso DOMPurify , u fanele ku endla leswi landzelaka:

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