[英]How can I run javascript in rails webpage
I am creating a webpage with rails and I'm trying to run baguetteBox.js to create an image gallery.我正在创建一个带有 rails 的网页,并且我正在尝试运行 baguetteBox.js 来创建一个图片库。 I've tried using <%= javascript_pack_tag 'gallery' %> at the top of the gallery page along with baguetteBox CSS and js script but it still not works.我尝试在图库页面顶部使用 <%= javascript_pack_tag 'gallery' %> 以及 baguetteBox CSS 和 js 脚本,但它仍然无法正常工作。 Hope that you can help me, thank you.希望你能帮助我,谢谢。
if in your javascript you are looking from an element to mount on or change on click it has to be imported after the html with the same tag just at the end of the body如果在您的 javascript 中,您正在寻找要安装或更改的元素,则必须在 html 之后导入,并在正文末尾使用相同的标签
To install baguetteBox need to run:要安装 baguetteBox 需要运行:
$ yarn add baguettebox.js
then you can create a file app/javascript/js/gallery.js
with the following:然后您可以使用以下内容创建文件app/javascript/js/gallery.js
:
import baguetteBox from 'baguettebox.js';
# If you are not using turbolinks replace 'turbolinks:load' with 'DOMContentLoaded'
document.addEventListener('turbolinks:load', () => {
baguetteBox.run('.gallery');
})
then you can import that into your application.js
file like so:然后您可以将其导入到您的application.js
文件中,如下所示:
...
import '../js/gallery'
That should get you started.那应该让你开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.