Rails - jQuery和javascript之间的冲突 [英] Rails - Conflicts between jQuery and javascript

查看:81
本文介绍了Rails - jQuery和javascript之间的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Gems加载到我的Rails 4应用程序时遇到问题。

I'm having a problem loading gems into my Rails 4 app.

我使用jQuery。在我的application.js中,我有:

I use jQuery. In my application.js, I have:

//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require html.sortable
//= require disqus_rails
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
//= require main
//= require hammer.min
//= require jquery.animate-enhanced.min
//= require jquery.countTo
//= require jquery.easing.1.3
//= require jquery.fitvids
//= require jquery.magnific-popup.min
//= require jquery.parallax-1.1.3
//= require jquery.properload
//= require jquery.shuffle.modernizr.min
//= require jquery.sudoSlider.min
//= require jquery.superslides.min
//= require masonry.pkgd.min
//= require rotaterator
//= require smoothscrolljs
//= require waypoints.min
//= require wow.min
//= require gmaps/google
//= require chosen-jquery
//= require cocoon
//= require imagesloaded.pkgd.min
//= require isotope.pkgd.min
//= require jquery.counterup.min
//= require jquery.pjax
//= require custom.js
//= require slider
//= require dependent-fields
//= require bootstrap-slider

$.noConflict();
jQuery( document ).ready(function( $ ) {
  // Code that uses jQuery's $ can follow here.
    $(document).ready(function() {
        DependentFields.bind()
    });
});


//= require_tree .

在我的gem文件中,我正在尝试使用rails dependent fields gem。那个gem还需要underscore.js。

In my gem file, I am trying to use rails dependent fields gem. That gem also requires underscore.js.

我认为这个gem不起作用的原因是因为它使用了javascript。

I think the reason this gem isn't working is because it uses javascript.

我遇到了与disqus rails gem相同的问题(这会引发localhost /的错误:94 Uncaught TypeError:$不是函数

I'm having the same problem with the disqus rails gem (which is raising errors that are localhost/:94 Uncaught TypeError: $ is not a function

有谁知道如何解决rails jQuery和javascript之间的冲突?

Does anyone know how to resolve conflicts between rails jQuery and javascript?

推荐答案

问题是你已经设置了jQuery来工作 noConflict 模式通过这一行: $ .noConflict(); .jQuery不会定义 $ 全局变量,仅可通过 jQuery 变量获得。

The problem is that you have set jQuery to work in noConflict mode through this line: $.noConflict();. jQuery will not define $ variable globally and will be available through jQuery variable only.

$.noConflict();   // <=====  HERE  ======
jQuery( document ).ready(function( $ ) {
  // Code that uses jQuery's $ can follow here.
    $(document).ready(function() {
        DependentFields.bind()
    });
});

引发此错误的插件可能需要设置 $ 变量。然而,他们不应该这样做。

The "plugins" that are raising this error are probably expecting $ variable to be set. They should not be doing that however.

你有2个选择:


  1. application.js 文件中删除 $。noConflict(); 行。

  2. 将引发此错误的代码包装到 $ 设置为 jQuery 变量。

  1. Remove $.noConflict(); line from your application.js file.
  2. Wrap the code that is raising this error in to a function where $ is set to jQuery variable.

喜欢这样:

(function($){
  // Here: $ === jQuery
  // Put js code that raises error here
})(jQuery);






此外,您使用的是2 文件代码中的就绪事件。你可以改进它:


Also, you are using 2 document ready events in your code. You can improve it to this:

$.noConflict();
jQuery( document ).ready(function( $ ) {
  // Code that uses jQuery's $ can follow here.
  DependentFields.bind()
});

这篇关于Rails - jQuery和javascript之间的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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