首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReferenceError:$是未定义的函数错误

ReferenceError:$是未定义的函数错误
EN

Stack Overflow用户
提问于 2012-12-05 13:41:19
回答 1查看 6K关注 0票数 0

我的jquery脚本遇到麻烦了,谁来计算旧推特上的字符数。我得到了ReferenceError: $ is not defined的指向

代码语言:javascript
复制
$(function() {

下面是我在rails应用程序上的脚本。

index.hml.erb

代码语言:javascript
复制
<% title "Welcome #{current_user.username}" %>

<script type="text/javascript">
    $(function() {
        $('#flit_message').keyup(function(){
            var content_lenght = $(this).val().length;
            var remaining = 140 - content_length
            $('#char_count').html(remaining);
        })
    })
</script>
<%= form_for Flit.new, :html => {:id => 'new_flit_form'}    do |f| %>
    <h3 style="float: left;">What are you doing?</h3>
    <h3 id="char_count" style="float: right;font-size: 23px; font-weight: bold; color: #aaa;">140</h3>
    <div class="clear"></div>
    <!-- <div id="new_flit_form"> -->
    <%=h f.text_area :message %>
    <div id="latest_message">
        <strong>Latest: </strong><%=h @last_flits.message %>
        <%= distance_of_time_in_words_to_now(@last_flits.created_at)%> ago
    </div>
    <div id="submit_button_container">
    <%=h f.submit "update", :class => "button"  %>
    </div>
    <div class="clear"></div>

    <!-- </div> -->
    <% end %>



    <ul id= "flits_list">
        <% @flits.each do |flit| %>
            <li<% if @flits.first == flit %> class="first"<% end %>>
            <%= image_tag flit.user.gravatar_url %>
            <div class="flit_message_container">
            <%=h link_to flit.user.username %>
              <%=h flit.message %>
            <div class="time_ago">
              <%= distance_of_time_in_words_to_now(flit.created_at)%> ago
            </div>
            </div>
            <div class="clear"></div>
            </li>
            <% end %>
    </ul>

这里是application.js文件

代码语言:javascript
复制
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .

html页面源代码

代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <title>Welcome breanne</title>
    <link href="/assets/application.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/flits.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/home.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/welcome.css?body=1" media="screen" rel="stylesheet" type="text/css" />
    <script src="/assets/defaults.js" type="text/javascript"></script>
    <meta content="authenticity_token" name="csrf-param" />
<meta content="JnHvVRrW5Ts6TQf+D35OByvXxEY+Gl6P5NU5dVs8r3o=" name="csrf-token" />

  </head>
  <body>
    <div id="container">
      <h1>Welcome breanne</h1>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" >
        $(function() {
            $('#flit_message').keyup(function(){
                var content_lenght = $(this).val().length;
                var remaining = 140 - content_length;
                $('#char_count').html(remaining);
            })
        })
    </script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-05 13:44:31

99%的情况下,这意味着JQuery没有在本节之前被包含到页面中。你把它包括在哪里?

像这样把它包括在头标签里

代码语言:javascript
复制
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" />

请参阅:http://jquery.com/download/

在加载页面、查看源代码并查看是否可以看到JQuery时,您可能希望取消对该文件中的jquery行的注释(删除//)。如果它不在那里,你的应用程序没有正确地包括它。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13724575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档