jquery没有在rails中工作 [英] jquery not working in rails

查看:82
本文介绍了jquery没有在rails中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多关于在rails中使用jquery的页面,但似乎无法让它工作。



我有'jquery-rails'宝石,我安装了。
我在application.js文件中有require语句。



这是我一直在运行的测试页:

 <!DOCTYPE html> 
< html>
< head>
< title><%= yield(:title)%>< / title>
<%= javascript_include_tag'application','jquery','data-turbolinks-track'=> true%>
<%= stylesheet_link_tag'application',media:'all','data-turbolinks-track'=> true%>
<%= csrf_meta_tags%>
< script> $(document).ready(function(){
$(。bg-info)。click(function(){
$(this).hide( );
});
});< / script>
< / head>
< body>
< center>
< h1 class =bg-info><%= yield(:title)%>< / h1>
< / center>
< / body>
< / html>

但是当我点击浏览器中的bg-info文字时,我没有得到回应。

解决方案

以下是所拥有的内容:

 #app / assets / javascripts / application.js 
// = require jquery
// = require jquery_ujs
// = require_tree。

$(document).on(click,。bg-info,function(e){
$(this).hide();
}) ;

这是我为布局做的事情(测试):

 #app / views / layouts / application.html.erb 
<!DOCTYPE html>
< html>
< head>
< title><%= yield(:title)%>< / title>
<%= javascript_include_tag'application','jquery','data-turbolinks-track'=> true%>
<%= stylesheet_link_tag'application',media:'all','data-turbolinks-track'=> true%>
<%= csrf_meta_tags%>
< / head>
< body>
< div class =bg-info><%= yield(:title)%>< / div>
< / body>
< / html>

如果上述方法无效,您可能您的



要访问开发控制台,右键单击>检查元素>控制台



这表明你的JS&你的前端环境。如果出现任何错误,他们可能会告诉您问题所在。



如果没有出现错误,您应该 尝试在系统上安装NodeJS ,因为这可以确保Rails可以使用相应的JS可执行文件。


I've read many pages about using jquery in rails and still can't seem to get it to work.

I have the 'jquery-rails' gem, and I installed. I have the require statements in the application.js file.

Here is a test page I keep running:

<!DOCTYPE html>
 <html>
 <head>
<title><%= yield(:title)%></title>
<%= javascript_include_tag 'application','jquery', 'data-turbolinks-track' => true  %>
<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<script>$(document).ready(function(){
  $(".bg-info").click(function(){
    $(this).hide();
});
 });</script>
</head>
<body>
<center>
<h1 class="bg-info"><%= yield(:title)%></h1>
</center>
</body>
</html>

But when I click on the "bg-info" text in the browser I get no response.

解决方案

Here's what you should have:

#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require_tree .

$(document).on("click", ".bg-info", function(e){
   $(this).hide();
});

Here's what I'd do for the layout (to test):

#app/views/layouts/application.html.erb
<!DOCTYPE html>
 <html>
   <head>
       <title><%= yield(:title)%></title>
       <%= javascript_include_tag 'application','jquery', 'data-turbolinks-track' => true  %>
       <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
       <%= csrf_meta_tags %>
   </head>
   <body>
       <div class="bg-info"><%= yield(:title)%></div>
   </body>
</html>

If the above doesn't work out the box, you'll probably have an issue with your development environment (OS) -- most likely with ExecJS.

To confirm this, you'll need to debug your developer's console:

To access the dev console, right-click > inspect element > console.

This shows you any errors with your JS & your front-end environment. If any errors appear, they may tell you what the issue is.

If no errors appear, you should try installing NodeJS on your system, as this ensures Rails can use the appropriate JS executable.

这篇关于jquery没有在rails中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆