为了使用 jQuery 和 jQuery UI,您需要按以下顺序完成三件事:
- jQuery UI CSS,以及任何自定义/主题
- jQuery 库;通常最小化空间
- jQuery UI 代码库,也已最小化。
为简单起见,以下是 CDN 中所有三个的示例:
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
这是包含它们的正常顺序,通常在您的 <head> 标记中,因此如果在(以后)头部包含中使用它,它可以工作。
考虑到所有这些,如果我使用此处自动完成页面中的示例,以下是可以工作的代码和序列:https://jqueryui.com/autocomplete/
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<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"
];
$( "#myinput" ).autocomplete({
source: availableTags
});
});
</script>
然后,在正文中我的输入标签:
<input id="myinput" type="text"/>
除非您需要支持较旧的浏览器,否则您只需要 2.X 版本(此处演示 2.2.2)如果您确实需要支持较旧的浏览器,您应该只需要 1.X 版本。为了证明它有效,我创建了这个:https://jsfiddle.net/MarkSchultheiss/op7Lq06g/
编辑:
您的问题中缺少的 HTTPS 怎么样:为了匹配您的网站,您将其从链接的标签中排除,它会根据 HTML 规范自动将其放入以匹配您网站的页面源。示例:
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
有关详细说明,包括指向该规范的链接,请参阅此问题:https://stackoverflow.com/a/36638189/125981