gpt4 book ai didi

javascript - jQuery - 未捕获的类型错误 : $ is not a function

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:41 25 4
gpt4 key购买 nike

我很困惑...我在我的网站上工作并添加了一些 jQuery。经过测试,我发现 jQuery 没有做任何事情。然后我尝试了我能想到的最简单但最可见的代码:

<script type="text/javascript">
$("html").click(function() {
$("html").html("");
});
</script>

但这也没有做任何事情。所以我在 js 控制台中尝试了相同的代码并返回了这个错误:

Uncaught TypeError: $ is not a function
at <anonymous>:2:1
at Object.InjectedScript._evaluateOn (<anonymous>:848:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:891:34)
at Object.InjectedScript.evaluate (<anonymous>:627:21)

只要输入 $ 就会返回“undefined”。

我调查后发现我不小心移动了我的 Gemfile,服务器可能无法访问 jquery-rails gem。但是在我将其移回并重新启动服务器后,问题并没有消失。

然后我想也许我的页面上有一些错误代码阻止了 jQuery 运行。我删除了所有代码,因此它只是一个空白文档,运行它并再次尝试,但我在控制台中遇到了同样的错误。

我的应用程序的 head 包括:

<head>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
</head>

我的 app/assets/javascripts/application.js 包含行 //= require jquery

bundle install 返回“jquery-rails 4.1.1”。

我之前在同一个页面上运行过大量 jQuery,没有出现任何问题。看来我的服务器突然停止理解 jQuery。到底发生了什么?

最佳答案

将其包装在一个 jQuery 函数中,如下所示:

// Note the "$" inside function() - that's the key
jQuery( document ).ready(function( $ ) {

$("html").click(function() {
$("html").html("");
});

});

关于javascript - jQuery - 未捕获的类型错误 : $ is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36526080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com