下拉菜单不起作用 jQuery 错误

Posted

技术标签:

【中文标题】下拉菜单不起作用 jQuery 错误【英文标题】:Dropdown not working jQuery error 【发布时间】:2016-02-17 12:45:42 【问题描述】:

我正在使用 Ruby 2.0.0 和 Rails 4.2.3。

我的引导下拉菜单在 heroku 上不起作用。我不知道为什么?我已经尝试了所有方法,但似乎没有任何帮助

我的 Gemfile

source 'https://rubygems.org'

gem 'rails', '4.2.3'
gem 'devise'
gem 'simple_form'
gem 'mailboxer'
gem 'chosen-rails'
gem 'acts_as_votable', '~> 0.10.0'
gem 'public_activity'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'coffee-script-source', '1.8.0'
gem 'paperclip', '~> 4.3.0'
gem 'bootstrap-sass', '~> 3.3.5.1'
gem "autoprefixer-rails"
gem 'font-awesome-sass', '~> 4.4.0'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

group :production do
    gem 'pg'
    gem 'rails_12factor'
end

group :development, :test do
    gem 'sqlite3'
end

# gem 'therubyracer', platforms: :ruby

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

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 any plugin's vendor/assets/javascripts directory 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
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//



//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require chosen-jquery
//= require turbolinks
//= require_tree .




$(document).ready(function()
    $(document).mousemove(function(e)
        TweenLite.to($('body'), 
            .5, 
             css: 
                
                    backgroundPosition: ""+ parseInt(event.pageX/8) + "px "+parseInt(event.pageY/'12')+"px, "+parseInt(event.pageX/'15')+"px "+parseInt(event.pageY/'15')+"px, "+parseInt(event.pageX/'30')+"px "+parseInt(event.pageY/'30')+"px"
                
            );
    );


);

$(function() 
  $("#shipment_date").datepicker();
);

application.html.erb

<!DOCTYPE html>
<html>
<head>
<head>
  <title>ShiftInd</title>
  <link href='http://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'>
  <%= stylesheet_link_tag     'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

  <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"></link>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
  <%= csrf_meta_tags %>
</head>
<body>
  <nav class="navbar navbar-default navbar-fixed-top">
    <div class="container-fluid">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <div class="col-md-12">
        <a class="navbar-brand" href="/shipments">Shiftind</a>
      </div>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">


      <ul class="nav navbar-nav navbar-right col-md-3">
        <% if user_signed_in? %>
        <li class="<%= active_page(:inbox) %>">
    <%= link_to mailbox_inbox_path do  %>
        <span class="label label-danger pull-right"><%=unread_messages_count%></span>
        <em class="fa fa-envelope fa-lg"></em>
    <% end %>
  </li>

  <li class="dropdown" >
        <span class="notification-count" style = "padding: 5px;"><%= @comment.count.inspect %></span>
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="notification-count">
            <span class="fa fa-bell fa-1x"></span></a>
            <ul class="dropdown-menu">
             <li><% @comment.flatten.each do |c|%>
               <%= link_to comments_notification_update_path(:comment_id => c.id), :class => "notification"  do %><%@user = User.find c.user_id%><%= @user.full_name.capitalize%> added a <i class="fa fa-inr"></i> bid <%= c.content.split(//).first(5).join %>....<% end %><br><br>
               <% end %>
             </li>        
           </ul>
         </li>

         <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><strong><font style="text-transform: capitalize;">Hi, <%= current_user.first_name %></strong></font><span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><%= link_to "Edit Your Details", edit_user_registration_path %></li>
            <li><%= link_to "Log Out", destroy_user_session_path, method: :get %><li>
              <% else %>
              <li><%= link_to "Register", new_user_registration_path %></li>
              <li><%= link_to "Log in", new_user_session_path %></li>
              <% end %>
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </nav>




  <div class="text-center">
    <% flash.each do |name, msg| %>
    <%= content_tag(:div, msg, class: "alert alert-info") %>
    <% end %>
  </div>
  <%= yield %>

</body>
</html>

当我在浏览器上检查 javascript 控制台 (F12) 时,它显示以下错误:

Error: Bootstrap's JavaScript requires jQuery application-6de83ae08cb7e1d68e66d4b193a2778e.js:9:9
GET 
ReferenceError: jQuery is not defined jquery.backstretch.min.js:1:1
ReferenceError: $ is not defined

我只是不知道如何解决这个问题。有人可以帮我吗?提前谢谢

【问题讨论】:

试试这个$(document).on('ready page:load', function() 做到了,但没有任何反应@anonymousxxx 您的代码中没有包含 bootstrap.min.js 文件。请添加该文件并检查一次。 做到了,但像往常一样没有发生任何事情@IamRaviteja &lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"&gt;&lt;/script&gt; 在 css 文件之后添加这个并确保您遵循外部文件包含的顺序 【参考方案1】:

您好像忘记在模板中包含对 JQuery 库的调用? 如此处示例中所述:http://getbootstrap.com/getting-started/#template

在您的views/layouts/application.html.erb 文件中的结束

以上是关于下拉菜单不起作用 jQuery 错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 JQuery 和 Flask 的动态下拉菜单不起作用

添加多个字段时,HTML多选下拉菜单Jquery和样式不起作用

简单的移动 jQuery 下拉菜单在移动设备上不起作用

Jquery Ui Datepicker 月/年下拉菜单在最新的 Firefox 中的弹出窗口中不起作用

Bootstrap 下拉菜单不起作用(对其他类似问题的回答不起作用!)

带引导程序的多个下拉菜单不起作用?