我到处寻找,但我无法弄清楚我的错误,因为我对javascript很新。问题是当页面第一次加载时,没有错误,所有的javascript工作正常,但当我点击“回家”时错误打印到控制台和javascript的部分' $(document).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>
另外,当我对此进行评论时,错误就会消失,所以问题出现在这段代码中,我无法弄清楚在哪里。
答案 0 :(得分:2)
你需要包括
<script
src="https://code.jquery.com/jquery-3.1.1.js"
integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
crossorigin="anonymous"></script>
代码