javascript - 我遗漏了一些小东西 : Uncaught ReferenceError: $ is not defined(anonymous function)

标签 javascript jquery html materialize

我正在尝试使用 Materialize Framework,但我似乎无法运行自动完成示例。这里缺少一些东西。

我在 Materialize 之前导入 jQuery,我什至尝试在 中移动 jQuery。如果你有建议。我知道它很蠢,但我就是看不到它,它在基本的事情上已经过去了 40 多分钟。

<!DOCTYPE html>
<html>
<head>
    <!--Import Google Icon Font-->
    <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!--Import materialize.css-->
    <link type="text/css" rel="stylesheet" href="css/materialize.css"  media="screen,projection"/>
    <!--Let browser know website is optimized for mobile-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>

<body>

<div class="row">

    <div class="col s12">
        <div class="row">
            <div class="input-field col s12">
                <i class="material-icons prefix">textsms</i>
                <input type="text" id="autocomplete-input" class="autocomplete">
                <label for="autocomplete-input">Autocomplete</label>

                <script>$('input.autocomplete').autocomplete({
                        data: {
                            "Apple": null,
                            "Microsoft": null,
                            "Google": 'http://placehold.it/250x250'
                        }
                    });
                </script>
            </div>
        </div>
    </div>
</div>


<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/materialize.js"></script>

</body>
</html>

谢谢!

最佳答案

我认为你的问题不在于物化,而在于

$('input.autocomplete').autocomplete({}) 

本身。您在实际导入之前尝试使用 $。

您应该将自己的代码移到 jQuery 之后并具体化导入语句,这样您就可以在自己的代码中使用 $ 并使 jQuery 可用。

(其他答案说将脚本移动到 head 标签。据我所知,这是一种不好的做法,您应该将所有 js 放在 body 标签的末尾)

关于javascript - 我遗漏了一些小东西 : Uncaught ReferenceError: $ is not defined(anonymous function),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39005161/

相关文章:

jquery - 使用 Jquery 从 html 表中拖放多行

jquery ui datepicker 选择后不会在 IE7 中消失

javascript - 如何在 HTML 标签中获取动态变量的值

html - Ruby on Rails - 显示来自 twitter feed 的最后 x 条推文

javascript - 尝试使输入框通过单击按钮出现 - Javascript

javascript - 如何读取多条记录的OData关联

javascript - 如何获取加载到网页上的.swf文件URL

Javascript 回文逻辑

javascript - 如果没有 child ,则删除一些元素

javascript - 服务器端表单数据的数据类型