简体   繁体   English

jQuery未随Ruby on Rails一起加载

[英]jQuery is not loading with Ruby on Rails

I'am having trouble by loading a Javascript file (which includes jQuery) in Ruby on Rails. 我在Ruby on Rails中加载Javascript文件(包括jQuery)时遇到了麻烦。

$(document).ready(main);

var contador = 1;

function main(){
  $('.menu_bar').click(function(){

    if(contador == 1){
      $('nav').animate({
        left: '0'
      });
      contador = 0;
     } else {
        contador = 1;
        $('nav').animate({
          left: '-100%'
        });
       }
    });
  };

The proper html.erb looks like this: 正确的html.erb看起来像这样:

<header>
  <div class="menu_bar">
    <a href="#" class="bt-menu"><span class="fa fa-bars"></span>Brand</a>
  </div>
  <nav>
    <ul>
      <li><%= link_to "Link 1", '#'%></li>
      <li><%= link_to "Link 2", '#'%></li>
      <li><%= link_to "Link 3", '#'%></li>
    </ul>
  </nav>
</header>

The proper application.js includes: 正确的application.js包括:

//= require rails-ujs
//= require turbolinks
//= require jquery
//= require jquery_ujs
//= require_tree .

jquery is sourced in the Gemfile with gem 'jquery-rails' . jQuery源自Gemfile中的gem'jquery -rails'

The animation is not loading after clicking. 单击后未加载动画。 I am using Rails 5 and I guess the problem is issued by Turbolinks. 我正在使用Rails 5,我想问题是由Turbolinks发出的。

What do I have to change to solve this problem? 为了解决这个问题,我必须更改什么?

Turbolinks tries to follow every link (even if it's a # , in which case it just reloads the page). Turbolinks尝试跟踪每个链接(即使它是# ,在这种情况下,它也只是重新加载页面)。 You have a few ways around this. 您有几种解决方法。

Option 1: Change your link to not have any href: 选项1:将链接更改为没有任何href:

<div class="menu_bar">
  <a class="bt-menu"><span class="fa fa-bars"></span>Brand</a>
</div>

Option 2: Stop the event from propagating, so that it doesn't reach turbolinks: 选项2:阻止事件传播,以使事件不会到达Turbolink:

$('.menu_bar a').click(function(e) {
  e.preventDefault();
  console.log("I HAVE BEEN CLICKED");
  // Rest of code here...
  return false;
});

Option 3: "Tell" turbolinks not to follow # links: 选项3: “告诉” turbolink不遵循#链接:

$(document).on('turbolinks:click', function (event) {
  if (event.target.getAttribute('href').charAt(0) === '#') {
    return event.preventDefault();
  }
});

If it doesn't work, add a few console.log within the code to find out which part it's not getting to. 如果它不起作用,请在代码中添加一些console.log ,以找出它不涉及的部分。 There might be some issues with the CSS (which is not shared so we can't help there), but it might require a relative or absolute position to move the nav . CSS可能存在一些问题(由于未共享,所以我们在那里无能为力),但可能需要相对或绝对位置来移动nav

Hopefully this sets you in the right direction, let me know if it doesn't work and what have you found out after debugging, and I'll update my answer. 希望这能为您设定正确的方向,让我知道它是否不起作用以及调试后您发现了什么,我将更新答案。

do like this, remove rails-ujs , require turbolink at last and require before turbolinks one by one all your files. 要做到这一点,删除rails-ujs ,最后require turbolink ,然后才需要一个一个地链接所有文件。 Do not use require . 不要使用require .

//= require jquery
//= require jquery_ujs
//= require each one of your files on the order you want to load the !!!
//= require turbolinks

Then include the js load function with this code 然后在此代码中包含js load函数

$(document).on('turbolinks:load', function() {
    main();
}

function main(){
    // Include your code
    };

Other question is, did you set a breakpoint inside this function and made sure it is not being called? 另一个问题是,您是否在此函数内设置了断点并确保未调用该断点?

$('.menu_bar').click(function(){
       // breakpoint
});

Yes, console.log('abc') is called correctly. 是的,正确调用了console.log('abc')。 – LSKoy – LSKoy

@LSKoy you are wrong. @LSKoy你错了。 If you have turbolinks working console.log will not be called correctly. 如果您具有Turbolinks,则无法正确调用console.log The $(document).ready(main) function will only call the main() function on full page refresh, because turbolinks caches the pages so when you click a link it does not do a full page refresh, so your $(document).ready does not run. $(document).ready(main)函数将仅在刷新整个页面时调用main()函数,因为涡轮链接会缓存页面,因此当您单击链接时,它不会进行整个页面刷新,因此您的$(document).ready无法运行。 This means that if you click a link , your $(document).ready does not run and does not execute function() { main(); } 这意味着,如果您单击link ,则$(document).ready不会运行并且不会执行function() { main(); } function() { main(); } which includes the js code you want to run function() { main(); } ,其中包含您要运行的js代码

to make correctly execute your js code on all turbolinks events you need to use 使您需要使用的所有turbolinks events上正确执行您的js代码

$(document).on('turbolinks:load', function() {
    main();
}

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

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