简体   繁体   English

jQuery没有在Rails 3.2.8上工作

[英]jQuery not working on Rails 3.2.8

I'm having an issue with jQuery on Rails 3.2.8. 我在Rails 3.2.8上遇到了jQuery的问题。 I've installed the jquery-rails gem and my application.js has the following: 我已经安装了jquery-rails gem,我的application.js有以下内容:

//= require jquery
//= require jquery_ujs
//= require_tree .

views/layouts/application.html.erb has views / layouts / application.html.erb有

<%= javascript_include_tag "application" %>

A simple Javascript test in test.js: test.js中的一个简单的Javascript测试:

if (jQuery) {
alert('Jquery is loaded')
} else {
alert ('Jquery is not loaded')
}

gives me an alert telling me that jQuery is indeed loaded. 给我一个警告,告诉我jQuery确实已加载。

However, no jQuery commands will work, even basic ones are unresponsive: 但是,没有jQuery命令可以工作,即使是基本的命令也没有响应:

$(document).ready(function(){
 $("a").click(function(event){
 alert("Thanks for visiting!");
});
});

I've tried everything -- even reincluding jQuery through the Google API. 我已经尝试了一切 - 甚至通过Google API重新使用jQuery。 Any ideas on what might be going on? 关于可能发生的事情的任何想法?

Fixed it! 固定它!

in application.html.erb, I changed the ordering of these two lines from: 在application.html.erb中,我改变了这两行的顺序:

<%= stylesheet_link_tag    "application", :media => "all" %>
<%= javascript_include_tag "application" %>

to: 至:

<%= javascript_include_tag "application" %>
<%= stylesheet_link_tag    "application", :media => "all" %>

Now it works! 现在它有效! I'm not quite sure why though, can anybody explain? 我不太清楚为什么,有人可以解释一下吗?

You probably want to check the console in the browser you are testing. 您可能想要在您正在测试的浏览器中检查控制台。

In jQuery's case, $ is just an alias for jQuery, does the following work 在jQuery的情况下,$只是jQuery的别名,做以下工作

jQuery(document).ready(function(){
   jQuery("a").click(function(event){
     alert("Thanks for visiting!");
   });
});

Perhaps you have called jQuery.noConflict() somewhere, removing the alias? 也许你已经在某处调用了jQuery.noConflict(),删除了别名?

http://api.jquery.com/jQuery.noConflict/ http://api.jquery.com/jQuery.noConflict/

Make sure you're including the javascript include tag before the yield in your application.html.erb . 确保在application.html.erbyield之前包含javascript include标记。 If you're using Bootstrap, I think the default application.rb loads JS after the yield, which makes it necessary to wait until jQuery is loaded before running it in your views. 如果您正在使用Bootstrap,我认为默认的application.rb在yield之后加载JS,这使得必须等到jQuery加载后再在视图中运行它。

If that's the case and you don't want to relocate the include tag higher on the page, you can do this: 如果是这种情况,并且您不想在页面上重新定位包含标记,则可以执行以下操作:

runScript();
function runScript() {
    if( window.$ ) { // Make sure jquery is loaded
         // your jquery code here
    } else {
        window.setTimeout(runScript, 100); // If not, wait 100 milliseconds and try again.
    }
}

Although its too late for an answer but still i'll provide as i was having the same problem and wasted like 2 hours on it. 虽然它的答案为时已晚,但我仍然提供,因为我有同样的问题,浪费了2个小时就可以了。 Remove the comments in your js file as they are ruby comments and not js comments. 删除js文件中的注释,因为它们是ruby注释而不是js注释。 If you remove them and write your js code, it'll work fine. 如果你删除它们并编写你的js代码,它将正常工作。

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

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