当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Error with adding JQuery

问题描述:

I added JQuery to my HTML file:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

Then I add a link to my JavaScript file:

<script src="public/javascripts/new_javascript.js" type="text/javascript"></script>

(I checked that this link work.)

In the file I do

$(document).ready(function() {

alert("hey!");

});

But the Google Chrome developer tool shows an error:

Uncaught TypeError: Object #<HTMLDocument> has no method 'ready'

How can it be an error?

I'm doing this in Rails, and the HTML is like:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script src="public/javascripts/prototype.js?1349898477" type="text/javascript"></script>

<script src="public/javascripts/effects.js?1349898477" type="text/javascript"></script>

<script src="public/javascripts/dragdrop.js?1349898477" type="text/javascript"></script>

<script src="public/javascripts/controls.js?1349898477" type="text/javascript"></script>

<script src="public/javascripts/rails.js?1349898477" type="text/javascript"></script>

<script src="public/javascripts/application.js?1349898477" type="text/javascript"></script>

<script src="public/javascripts/new_javascript.js?1351137775" type="text/javascript"></script>

网友答案:

jQuery and Prototype conflict with each other. Try using jQuery instead of $ for jquery commands:

jQuery(document).ready(function() {
   alert("hey!");
});
网友答案:

If prototype still overwrites $ like it's the dark ages, wrap your JQ code in a function like so:

(function($) {

    // Do whatever you like with $

    $(function() {
        alert("Document ready");
    })

})(jQuery);
网友答案:

You can use jQuery.noConflict()

$.noConflict();
jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
});
// Code that uses other library's $ can follow here.
分享给朋友:
您可能感兴趣的文章:
随机阅读: