简体   繁体   English

如何在 rails 网页中运行 javascript

[英]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.

相关问题 我怎样才能在我的网页中只运行一次 JavaScript? - How can i run JavaScript inside my webpage only once? 我该如何插入<script type='text/javascript' src=''> tag on a 3rd party webpage using rails? - How can I insert a <script type='text/javascript' src=''> tag on a 3rd party webpage using rails? 我如何获取URL中的电子邮件地址,以使用javascript或HTML以及基于Ruby on Rails构建的网站出现在我的网页上? - How can I get the email address in a URL to appear on my webpage with javascript or HTML with a website built on Ruby on Rails? 如何在Java应用程序中打开网页并运行自己的JavaScript代码 - How can I open a webpage within a Java app and run my own javascript code 单击ASP.net控件时,如何使我的网页运行JavaScript代码? - How can I make my webpage run JavaScript code when an ASP.net control is clicked? 如何在网页上重新加载Javascript文件? - How can I reload a Javascript file on a webpage? 如何使用 JavaScript 截取网页截图? - How can I capture webpage screenshots with JavaScript? 我可以在PhantomJS中运行网页的JavaScript函数吗? - Can I run webpage's JavaScript function in PhantomJS? 如何使用JavaScript在网页上运行正则表达式 - How to run regex on webpage with javascript 如何打开网页并运行一些javascript函数? - How do I open a webpage and run some javascript functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM