javascript - 我应该把 JavaScript 代码放在哪里?

标签 javascript jquery autocomplete

我想使用自动完成代码。密码是here .

<script>
    $(function() {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $( "#tags" ).autocomplete({
            source: availableTags
        });
    });
    </script>



<div class="demo">

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags">
</div>

</div><!-- End demo -->



<div class="demo-description" style="display: none; ">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-option.</p>
</div><!-- End demo-description -->

但是,我不知道应该把这段代码放在哪里。在头上?在体内?

最佳答案

根据 w3schools

何时将脚本放在 HEAD 中

Scripts to be executed when they are called, or when an event is triggered, are placed in functions. Put your functions in the head section, this way they are all in one place, and they do not interfere with page content.

什么时候把脚本放在BODY中

If you don't want your script to be placed inside a function, or if your script should write page content, it should be placed in the body section.

所以在你的情况下。您可以将脚本放在 body

关于javascript - 我应该把 JavaScript 代码放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4647806/

相关文章:

javascript - 使用 jQuery 更改 iFrame 背景

Javascript 和 jquery 语法

javascript - 在 Jquery 函数中包含 html 代码

javascript - WebStorm TypeScript 减少自动完成建议

javascript - Coda 2 中 Javascript 和 Jquery 的自动完成提示

php - 如何使用javascript检查是否选择了文件?

php - 在 jQuery uploadify 中,在用户的文件路径上传之前将图像显示为缩略图

javascript - 根据数组元素调用函数

javascript - 调整容器的大小,两个灰色方 block 应该尽可能地增长,受对 Angular 线的约束..简单的方法吗?

vim - 在新的 vim 命令中启用文件名完成