问题描述
我相信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);
有一个出色的投射在涡轮链接上的轨道
这篇关于Rails Jquery在其他页面上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!