无法运行自动完成代码

时间:2016-04-14 06:33:21

标签: jquery html jquery-ui jquery-autocomplete

我从Jquery复制了以下代码。当我尝试在我的系统上运行此代码时,没有任何操作,我是否必须更改标头标记中的脚本中的任何内容?

以下是我复制的代码:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>autocomplete demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body> 
<label for="autocomplete">Select a programming language: </label>
<input id="autocomplete"> 
<script>
var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ];
$( "#autocomplete" ).autocomplete({
  source: function( request, response ) {
          var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
          response( $.grep( tags, function( item ){
              return matcher.test( item );
          }) );
      }
});
</script> 
</body>
</html>

我是Jquery的新手,我不知道在标题中继承脚本文件,你能解释一下如何做到这一点吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

您的代码很好,但有一种可能性是使用相对协议来包含jQuery和jQuery UI资源。

如果您使用文件协议加载html页面(地址栏中的页面网址以file://开头)不使用httphttps等网络协议,则系统不会能够找到远程资源。因此,加载jQuery和UI将失败,导致脚本在页面中失败。

相反,您可以尝试通过指定完整的uri来加载外部资源

<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>