我错过了一些小的东西:未捕获的ReferenceError:$未定义(匿名函数)

Dan*_*iel 1 html javascript jquery materialize

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

我在Materialize之前导入了jQuery,我甚至尝试在<head>中移动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>
Run Code Online (Sandbox Code Playgroud)

谢谢!

Ser*_*eon 5

我认为你的问题不是实现,而是实现

$('input.autocomplete').autocomplete({}) 
Run Code Online (Sandbox Code Playgroud)

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

您应该移动自己的代码来执行jQuery并实现import语句,这样您就可以在自己的代码中使用$并使jQuery可用.

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