简体   繁体   English

引导程序 5:使用 javascript 显示模态

[英]Bootstrap 5 : show modal with javascript

I'm making a neat gallery with bootstrap 5 , and I wondered how I could trigger a bootstrap modal without using "data-bs-..." in my HTML (to avoid duplicating those data- attributes 50 times).我正在用bootstrap 5制作一个整洁的画廊,我想知道如何在我的 HTML 中不使用“data-bs-...”的情况下触发引导模式(以避免将这些数据属性复制 50 次)。

I managed to get a full functionning javascript for the sources, but the.modal() functions and.show() functions don't seem to work.我设法为源获得了一个功能齐全的 javascript,但是 .modal() 函数和 .show() 函数似乎不起作用。 Here's the thing:事情是这样的:

function gallery(_src) {
var fullPath = _src;
var fileName = fullPath.replace(/^.*[\\\/]/, '');
var newSrc = "./assets/img/" + fileName;
document.querySelector("div#galleryModal img").src = "./assets/img/" + fileName;

document.getElementById("galleryModal").show(); }

I'm blocked on this last javascript line.我在最后一条 javascript 线上被阻止了。 The objective: trigger #galleryModal.目标:触发#galleryModal。

Thanks for reading, thanks for helping !感谢阅读,感谢帮助!

We need to Create a modal instance if we want to operate with javascript check the below code and you find more info https://getbootstrap.com/docs/5.0/components/modal/#via-javascript如果我们想使用 javascript 操作,我们需要Create a modal instance检查下面的代码,你会发现更多信息https://getbootstrap.com/docs/5.0/components/modal/#via-javascript

var galleryModal = new bootstrap.Modal(document.getElementById('galleryModal'), {
  keyboard: false
});


galleryModal.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM