繁体   English   中英

如何使用 jQuery hover() 方法 Ruby on Rails 6.0.2.2 版本

[英]How to use jQuery hover() Method for Ruby on Rails 6.0.2.2 version

我想在轨道上学习 ruby 并且我正在处理一些项目。 当我用鼠标在照片上 hover 时,我想看到一个解释。 我知道,我如何使用 jquery hover 但只有轨道5.2 版本但我有轨道6.0.2.2 版本。现在我如何使用 ZE0542F579DF8E8138ADE69F6F8 版本的轨道您可以在屏幕截图上看到错误消息和我的照片。 最后感谢您的帮助

错误截图= [[1]: https://i.stack.imgur.com/CjTUF.png]

jquery Hover Shot.js的相关代码行

shotHover() {
                $('.shot').hover(function() {
                    $(this).children('.shot-data').toggleClass('visible');
                });
            }

Shots.shotHover();

应用程序.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.

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("shots")



// 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)

索引.html.erb

<%= link_to shot, class: "shot" do %>
          <%= image_tag(shot.user_shot_url) %>
          <div class="shot-data">
            <h3 class="shot-title"><%= shot.title %></h3>
            <div class="shot-description"><%= truncate(shot.description, length: 60) %></div>
            <div class="shot-time">
              <%= time_ago_in_words(shot.created_at) %>
            </div>
          </div>
        <% end %>

我在 Gemfile 中有这样的 jquery。 宝石文件

gem 'jquery-rails', '~> 4.3', '>=4.3.1'

我找到了解决方案。 Rails 6 有一些新的创新。

在我们的 Rails 应用程序中运行以下命令以添加 jQuery。

$ yarn add jquery

它将 jquery 的依赖关系保存到我们的应用程序中。

现在验证 jquery 是否安装,检查以下文件

package.json => "jquery": "^3.3.1",
yarn.lock => jquery@^3.3.1:

在 environment.js 中添加以下代码

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

现在,我们的文件看起来像,

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

module.exports = environment

在 application.js 文件中需要 jquery。 它看起来像,

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")

就是这样。您可以在您的项目中使用 jquery

暂无
暂无

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

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