2017-02-05 171 views
0

我到處找,但我想不出我的錯誤,因爲我很新的JavaScript。問題是,當第一次加載頁面時,沒有錯誤,所有的JavaScript都可以正常工作,但是當我點擊'home'時,錯誤被打印到控制檯和'$(document)下的javascript部分「 .ready'不再有效。

這裏的錯誤:

Uncaught TypeError: $ is not a function 
at <anonymous>:2:7 
at n.t.SnapshotRenderer.n.assignNewBody (turbolinks.self-c5acd7a….js?body=1:6) 
at n.t.SnapshotRenderer.n.replaceBody (turbolinks.self-c5acd7a….js?body=1:6) 
at turbolinks.self-c5acd7a….js?body=1:6 
at n.t.Renderer.t.renderView (turbolinks.self-c5acd7a….js?body=1:6) 
at n.t.SnapshotRenderer.n.render (turbolinks.self-c5acd7a….js?body=1:6) 
at Function.t.Renderer.t.render (turbolinks.self-c5acd7a….js?body=1:6) 
at e.t.View.e.renderSnapshot (turbolinks.self-c5acd7a….js?body=1:6) 
at e.t.View.e.render (turbolinks.self-c5acd7a….js?body=1:6) 
at r.t.Controller.r.render (turbolinks.self-c5acd7a….js?body=1:6) 
(anonymous) @ VM6247:2 
t.SnapshotRenderer.n.assignNewBody @ turbolinks.self-c5acd7a….js?body=1:6 
t.SnapshotRenderer.n.replaceBody @ turbolinks.self-c5acd7a….js?body=1:6 
(anonymous) @ turbolinks.self-c5acd7a….js?body=1:6 
t.Renderer.t.renderView @ turbolinks.self-c5acd7a….js?body=1:6 
t.SnapshotRenderer.n.render @ turbolinks.self-c5acd7a….js?body=1:6 
t.Renderer.t.render @ turbolinks.self-c5acd7a….js?body=1:6 
t.View.e.renderSnapshot @ turbolinks.self-c5acd7a….js?body=1:6 
t.View.e.render @ turbolinks.self-c5acd7a….js?body=1:6 
t.Controller.r.render @ turbolinks.self-c5acd7a….js?body=1:6 
(anonymous) @ turbolinks.self-c5acd7a….js?body=1:6 
(anonymous) @ turbolinks.self-c5acd7a….js?body=1:6 

出現這種情況,我不是點擊文章或主頁的每一個時間,但不會發生其他頁面項目一樣,和接觸。

這裏是我的javascript代碼:

<script type="text/javascript"> 
    $(document).ready(function() { 
    console.log("called"); 
    $(".fa-times").click(function() { 
     $(".sidebar_menu").addClass("hide_menu"); 
     $(".sidebar_menu").addClass("opacity_one"); 
     $(".bodywrapper").removeClass("push"); 
     $(".bodywrapper").addClass("pushback"); 
    }); 

    $(".toggle_menu").click(function() { 
     $(".sidebar_menu").removeClass("hide_menu"); 
     $(".sidebar_menu").removeClass("opacity_one"); 
     $(".bodywrapper").addClass("push"); 
     $(".bodywrapper").removeClass("pushback"); 
    }); 
    }); 

    var num = 1; 
    function back() { 
    if (num == 1) { 
     $("#project-1").hide(); 
     $("#project-3").show(); 
     $("#project-2").hide(); 
     $("#a-1").hide(); 
     $("#a-3").show(); 
     $("#a-2").hide(); 
     num = 3; 
    } else if (num == 2) { 
     $("#project-3").hide(); 
     $("#project-1").show(); 
     $("#project-2").hide(); 
     $("#a-1").show(); 
     $("#a-2").hide(); 
     $("#a-3").hide(); 
     num--; 
    } else if (num == 3) { 
     $("#project-1").hide(); 
     $("#project-3").hide(); 
     $("#project-2").show(); 
     $("#a-3").hide(); 
     $("#a-2").show(); 
     $("#a-1").hide(); 
     num--; 
    } 
    } 
    function resize() { 
    if ($(window).width() > 955) { 
     $("#a-1").show(); 
     $("#a-2").show(); 
     $("#a-3").show(); 
     $("#project-1").show(); 
     $("#project-3").show(); 
     $("#project-2").show(); 
    } else { 
     $("#a-1").show(); 
     $("#a-2").hide(); 
     $("#a-3").hide(); 
     $("#project-1").show(); 
     $("#project-3").hide(); 
     $("#project-2").hide(); 
    } 
    } 

    function myfunction() { 
    if (num == 1) { 
     $("#project-1").hide(); 
     $("#project-3").hide(); 
     $("#project-2").show(); 
     $("#a-1").hide(); 
     $("#a-3").hide(); 
     $("#a-2").show(); 
     num++; 
    } else if (num == 2) { 
     $("#project-3").show(); 
     $("#project-1").hide(); 
     $("#project-2").hide(); 
     $("#a-1").hide(); 
     $("#a-2").hide(); 
     $("#a-3").show(); 
     num++; 
    } else if (num == 3) { 
     $("#project-1").show(); 
     $("#project-3").hide(); 
     $("#project-2").hide(); 
     $("#a-3").hide(); 
     $("#a-2").hide(); 
     $("#a-1").show(); 
     num = 1; 
    } 
    } 
</script> 

此外,當我評論了這一點,錯誤消失,所以這個問題是在此代碼,我只是想不通的地方。

+4

你忘了導入jQuery的。 – Pointy

+0

或可能使用jQuery.noConflict()在某處。或者顯示的代碼插入jQuery,js加載之前的頁面中。您的錯誤很容易在網絡上搜索,以找出可能發生的各種原因 – charlietfl

+0

如果在調用腳本之前未加載jQuery,就會發生這種情況。 jQuery是在頁面的頭部加載的,並且這個腳本是否在底部? –

回答

1

你需要包括

<script 
    src="https://code.jquery.com/jquery-3.1.1.js" 
    integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" 
    crossorigin="anonymous"></script> 

在你的代碼

+0

謝謝你這個工作! – Mia

+2

@Mia這個問題在SO上每天回答三次。可能http://google.it在問這裏之前... –

+0

@Jonasw哈哈,你還算它XD – plonknimbuzz

相關問題