javascript - rails : Can't load two Jquery scripts in my page

标签 javascript jquery ruby-on-rails ruby algolia

我的 Rails 应用程序有问题 - 我有两个脚本使彼此无法正常工作。首先,algolia 搜索引擎脚本(运行良好)

在 app/assets/javascript/algolia.js 中:

  $(document).ready(function() {
    // Replace the following values by your ApplicationID and ApiKey.
    var algolia = new AlgoliaSearch('MYAPPID', 'MYAPPPWD');
    // replace YourIndexName by the name of the index you want to query.
    var index = algolia.initIndex('Pin');

    // Mustache templating by Hogan.js (http://mustache.github.io/)
    var template = Hogan.compile('<div class="hit">' +
      '<a href="http://0.0.0.0:3000/pins/{{{slug}}}">'
     +
      '<div class="name">' +
        '{{{ _highlightResult.description.value }}} ' +
      '</div>' +
      '</a>' +
      '</div>');


    // typeahead.js initialization
    $('#user-search').typeahead(null, {
      source: index.ttAdapter({ hitsPerPage: 5 }),

      displayKey: 'description',
      templates: {
        suggestion: function(hit) {

          // select matching attributes only
          hit.matchingAttributes = [];
          for (var attribute in hit._highlightResult) {
            if (attribute === 'name' || attribute == 'company') {
              // already handled by the template
              continue;
            }
            // all others attributes that are matching should be added in the matchingAttributes array
            // so we can display them in the dropdown menu. Non-matching attributes are skipped.
            if (hit._highlightResult[attribute].matchLevel !== 'none') {
              hit.matchingAttributes.push({ attribute: attribute, value: hit._highlightResult[attribute].value });
            }
          }

          // render the hit using Hogan.js
          return template.render(hit);

        }
      }
    });
  });

还有一个使用 magnific-popup-gem 加载弹出窗口的脚本

 $(function() {
          $('.please-login').magnificPopup({
          type: 'inline',
          preloader: false,
          focus: '#username',
          modal: true
          });
          $(document).on('click', '.please-login-dismiss', function (e) {
          e.preventDefault();
          $.magnificPopup.close();
          });
          });

看起来这些行使宏伟的弹出窗口不起作用。

 <script src="//cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>
    <!-- Typahead.js is used to display the auto-completion menu -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.10.4/typeahead.bundle.min.js"></script>
    <!-- Hogan.js is used to render the hits using Mustache.js templating -->
    <script src="//cdn.jsdelivr.net/hogan.js/3.0.0/hogan.common.js"></script>
    <!-- Algolia -->
    <script src="//cdn.jsdelivr.net/algoliasearch/latest/algoliasearch.min.js"></script>

有什么办法可以解决这个问题吗?

最佳答案

请检查 jquery 在每个依赖它的脚本之前只加载一次。

Jquery 应该已经被 rails 加载所以你可能不需要这一行

<script src="//cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>

关于javascript - rails : Can't load two Jquery scripts in my page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828487/

相关文章:

Javascript用用户给定的行和列制作表格并更改每个单元格的颜色onclick

javascript - 如何使用 C# 验证 Microsoft MVP 个人资料链接是否有效?

ruby-on-rails - 如何使用 SecureRandom.urlsafe_base64?

javascript - 使用元素数据集作为变量动态调用函数

javascript - 从 Controller 发送两个参数到 Ajax |网络

javascript - 使用 ExtJS 创建对象时出错

php - WordPress "admin-ajax.php"404 错误

javascript - 即使禁用,Tablesorter 在编辑行项目时也会更改行位置

javascript - Raty jQuery 插件无法正常工作 Rails 5

ruby-on-rails - 我如何才能只抓取 20 分钟前创建的带有评论的文章?