我可能在做一些愚蠢的事情,当我看到答案时,我会感到很尴尬,但我无法让最简单的EasyAutocomplete示例发挥作用。下面是我的完整代码,基于http://easyautocomplete.com/guide上的"Basics“示例:
<head>
<!-- jQuery -->
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<!-- JS file -->
<script src="jquery.easy-autocomplete.min.js"></script>
<!-- CSS file -->
<link rel="stylesheet" href="easy-autocomplete.min.css">
<script>
var options = {
data: ["blue", "green", "pink", "red", "yellow"]
};
$("#basics").easyAutocomplete(options);
</script>
</head>
<body>
<h2>Search</h2>
<input id="basics" />
</body>这是从本地文件运行的,而不是web服务器。我已经验证了浏览器可以加载所有脚本和css文件,并且没有错误。但当我打字的时候什么都不会发生。EasyAutocomplete版本为1.3.5。
发布于 2021-05-12 21:16:47
问题是easyAutocomplete脚本块缺少$(document).ready()。引用jQuery文档的话,“在文档”准备好之前,不能安全地操作页面。jQuery为您检测到这种准备状态。$(document).ready()中包含的代码只有在页面文档对象模型(DOM)准备好供JavaScript代码执行时才能运行。
下面是完整的工作示例。我还更新了最新的jQuery版本,但这不影响功能。
<head>
<!-- jQuery -->
<script src="http://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- JS file -->
<script src="jquery.easy-autocomplete.min.js"></script>
<!-- CSS file -->
<link rel="stylesheet" href="easy-autocomplete.min.css">
<script>
$(document).ready(function() {
var options = {
data: ["blue", "green", "pink", "red", "yellow"]
};
$("#basics").easyAutocomplete(options);
});
</script>
</head>
<body>
<h2>Search</h2>
<input id="basics" />
</body>https://stackoverflow.com/questions/67509910
复制相似问题