簡體   English   中英

Rails 6:未捕獲的引用錯誤 - $ 未定義

[英]Rails 6: Uncaught Reference Error - $ is not defined

我正在嘗試讓語義 UI 按鈕在 Rails 6 中使用 Javascript 工作。但是,我的代碼都沒有正常工作。 代碼如下所示:

$(document).on('turbolinks:load', function() {
   $('.message .close').on('click', function() {
   $(this).closest('.message').transition('fade');
   })
;
})

我得到的錯誤是:Uncaught ReferenceError: $ is not defined。

我的應用程序 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)
Uncaught ReferenceError: $ is not defined

此錯誤引用 $ 尚未聲明,最終表示 jquery 未正確加載到頁面中

檢查gemfile中是否添加了jquery gem

另外,嘗試是否使用jQuery作為標識符而不是$

在您的 html 頁面中添加 jquery 支持文件。

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM