TypeError:$(...)。不是函数,如何在加载jQuery插件后执行该函数

时间:2014-07-12 04:56:08

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

我在头部加载了jQuery插件,

然后在DOM就绪时运行该函数,

但是,我总是得到TypeError: $(...).jSlots is not a function

index.html.haml

  %head
    = javascript_include_tag "application" 
    %script{charset: "utf-8", src: asset_path("jslot/jquery.jSlots.js"), type: "text/javascript"}    

jslot / jquery.jSlots.js

(function($){

    $.jSlots = function(el, options){
    ~~~

index.js.coffescript

$(document).ready ->
  $('.fancy .slot').jSlots
      number : 1,
      winnerNumber : 1,
      spinner : '#playFancy',
      easing : 'easeOutSine',
      time : 7000,
      loops : 6,
      onStart:
        ->
          $('.slot').removeClass('winner')
      onWin:
        (winCount, winners) ->
          $.each winners, ->
            @addClass "winner"
            return
          if winCount==1
            console.log "hi"
          else if winCount > 1
            console.log "hi"
          return

1 个答案:

答案 0 :(得分:0)

似乎你不包括jquery本身!

试试这个:

%head
    = javascript_include_tag "application"         
    %script{charset: "utf-8", src: "http://code.jquery.com/jquery-1.11.1.js", type: "text/javascript"}
    = javascript_include_tag "application"
    %script{charset: "utf-8", src: asset_path("jslot/jquery.jSlots.js"), type: "text/javascript"}

我不是一个红宝石devlopper,不使用HAML,随时纠正答案。