Rails Jquery 在其他页面上不起作用 [英] Rails Jquery doesn't work on other pages

查看:27
本文介绍了Rails Jquery 在其他页面上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信 jQuery 不能在您刚安装的页面之外的其他页面上运行.例如,当我输入 localhost:3000/时,'/' 目录中的所有 jQuery 都可以工作.但是当我点击由 Rails 创建的链接时

I believe jQuery doesn't work on other pages than the one you just installed. For example, when I type localhost:3000/, in the '/' directory all jQuery works. But when I click to a link created by Rails as

<%= link_to "Example Link", news_path %>

页面正确加载,但 jQuery 不起作用.我的 jQuery 代码如下所述:

The page correctly loads, but the jQuery doesn't work. My jQuery codes are as stated:

$(function() {
  console.log( "pageloaded" );
  ....
  $('.up').click(function(){
    .....
  });
});

推荐答案

在 Rails 4 中 turbolinks 默认处于活动状态.
这意味着,当您加载新页面时,不会执行 $(document).ready().

In Rails 4 turbolinks is active by default.
That means, that $(document).ready() is not executed, when you load a new page.

turbolink 触发一个新事件 page:load.您可以使用它来运行您的 javascript 代码:

turbolink fires a new event page:load. You can use this to run your javascript code:

$(document).on('page:load', your_start_function);

有一个很好的在turbolinks上投射的rails

这篇关于Rails Jquery 在其他页面上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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