本文介绍了使用Turbolink插件加载JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有jQuery-turbolinks.但是无论如何,都可以将jQuery和JS与Turbolink结合使用.

I know there is jQuery-turbolinks. But is there anyway to use jQuery and JS with turbolink.

https://github.com/rails/turbolinks

使用Turbolinks的页面将在没有完全重新加载的情况下发生更改,因此您不能依靠DOMContentLoadedjQuery.ready()来触发代码.相反,Turbolinks在文档上触发事件以提供页面生命周期的挂钩.

With Turbolinks pages will change without a full reload, so you can't rely on DOMContentLoaded or jQuery.ready() to trigger your code. Instead Turbolinks fires events on document to provide hooks into the lifecycle of the page.

我有这段代码,但是没有在navigating上加载到其他URL.

I had this piece of code, but this did not load on navigating to other url.

$(function() {
  return $('.status').hover(function(event) {
    return $(this).toggleClass("hover");
  });
});

推荐答案

您可以执行以下操作在页面上同时运行JavaScript:加载并准备就绪.

You can do the following to run your javascript on both page:load and ready.

$(document).on('page:load ready', function() {
    ...
});

这篇关于使用Turbolink插件加载JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 10:24