jQuery UI自动完成功能不适用于我

时间:2019-06-01 20:56:42

标签: javascript jquery html jquery-ui-autocomplete

我需要一个项目的jQuery自动完成功能,但对我来说不起作用。 我不知道我的错误在哪里。 预先感谢!

<!DOCTYPE html>
<html>
<head>
    <meta charset="iso-8859-1" />
    <title>Votre titre</title> 

    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/themes/smoothness/jquery-ui.css" />
</head>
<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>
    <script>
        var liste = [
            "Draggable",
            "Droppable",
            "Resizable",
            "Selectable",
            "Sortable"
        ];

        $('#recherche').autocomplete({
            source : liste
        });
    </script>

    <form>
        <input type="text" id="recherche" />
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

此处的html语法存在多个问题:

  • 将jquery文件的脚本标签移动到head标签
  • 将脚本移到正文之后

您的脚本必须位于body标签之后,或者可以放置它而不是head标签,并使用文档就绪检查以使其在页面加载后运行(请参阅http://learn.jquery.com/using-jquery-core/document-ready/)。 $( document ).ready()中包含的代码仅在页面文档对象模型(DOM)准备好执行JavaScript代码后才能运行。

    <!DOCTYPE html>
<html>
<head>
    <meta charset="iso-8859-1" />
    <title>Votre titre</title> 
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/themes/smoothness/jquery-ui.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>
</head>
  <body>
      <form>
          <input type="text" id="recherche" />
      </form>
  </body>
    <script>
        var liste = [
            "Draggable",
            "Droppable",
            "Resizable",
            "Selectable",
            "Sortable"
        ];

       $('#recherche').autocomplete({
            source : liste
        });
    </script>
</html>

在此处https://jsfiddle.net/q7k28bp0/1/

相关问题