javascript - 引用错误: $ is not defined function error

标签 javascript jquery ruby-on-rails ruby-on-rails-3

我的 jquery 脚本遇到了麻烦。谁应该用旧的 twitter 方式来计算字符数。我收到 ReferenceError: $ is not Defined 指向

$(function() {

这是我在 Rails 应用程序上的脚本。

index.hml.erb

<% 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 文件

// 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页面源代码

<!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>

最佳答案

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

像这样将其包含在 head 标签内

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" />

参见:http://jquery.com/download/

您可能想要取消注释该文件中的 JQuery 行(删除//) 当您加载页面时,查看源代码并查看是否可以看到 jquery include。如果它不存在,您的应用程序就没有正确包含它。

关于javascript - 引用错误: $ is not defined function error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13724575/

相关文章:

ruby-on-rails - 使用 Amazon SES 对 Rails 应用程序进行 DKIM 签名

jquery - 如何在rails 4应用程序中安装jquery-ui/datepicker?

javascript - 在悬停时更改整个 <li> 颜色,而不仅仅是 href

ASP.NET:如何从 JScript 中访问 UserControl 的属性?

javascript - 使用 javascript 下拉列表

javascript - 当它出现在视口(viewport)中时开始 SVG 动画

jquery - 如何使用 Express 返回格式正确的 201?

javascript - 动态更改 iframe 内的元素样式

javascript - 修复了表格和 div 内的页眉和页脚?

ruby-on-rails - 使用用户设置为number_to_currency设置单位?