我试图在Rails 6中使用Javascript来获得一个语义UI按钮,但是,我的代码都没有正常工作。代码如下所示:
$(document).on('turbolinks:load', function() {
$('.message .close').on('click', function() {
$(this).closest('.message').transition('fade');
})
;
})我得到的错误是:未定义的ReferenceError:$。
我的应用程序JS文件看起来如下:
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
// Loads all Semantic javascripts
//= require jquery
//= require semantic-ui
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
//
// const images = require.context('../images', true)
// const imagePath = (name) => images(name, true)发布于 2019-12-03 21:54:43
Uncaught ReferenceError: $ is not defined这个错误引用还没有声明$,这最终表示jquery没有正确加载到页面中。
检查是否将jquery添加到gem文件中。
另外,尝试是否使用jQuery作为标识符而不是使用$。
发布于 2019-12-04 04:30:12
在html页面中添加jquery支持文件。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>https://stackoverflow.com/questions/59165827
复制相似问题