放置在外部脚本文件中时,Javascript 不执行
我正在构建一个 ASP.NET MVC 网站,我需要一个标签编辑器,类似于 Stack Overflow 上使用的标签编辑器。我已经研究过如何使用 jQuery UI 完成必要的自动完成,但遇到了一个问题:当我将脚本放入外部 .js
文件中时,它不会执行。
这是我的 test.html
:
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script src="http://jqueryui.com/jquery-1.4.2.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.core.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.widget.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.position.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.autocomplete.js"></script>
<script src="jquery.tagautocomplete.js"></script>
<script>
$(function() { bindAutoTagComplete('#birds'); })
</script>
</head>
<body>
<label for="birds">Birds: </label>
<input id="birds" size="50" />
</body>
</html>
这是 jquery.tagautocomplete.js
:
function bindAutoTagComplete(item, otherRootDomain)
{
function split( val ) {
return val.split( / \s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$(item).autocomplete({
source: function( request, response ) {
$.getJSON('http://jqueryui.com/demos/autocomplete/search.php', {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( " " );
return false;
}
});
}
您认为可能导致此问题的原因是什么? 我可能错过了.js
文件中的一些右括号/大括号...
提前致谢!
I'm building an ASP.NET MVC site where I need a tag editor, similar to the one used on Stack Overflow. I've already looked up how to accomplish the necessary autocompletion with jQuery UI, but I've run into a problem: when I place the script in an external .js
file, it doesn't execute.
Here is my test.html
:
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script src="http://jqueryui.com/jquery-1.4.2.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.core.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.widget.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.position.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.autocomplete.js"></script>
<script src="jquery.tagautocomplete.js"></script>
<script>
$(function() { bindAutoTagComplete('#birds'); })
</script>
</head>
<body>
<label for="birds">Birds: </label>
<input id="birds" size="50" />
</body>
</html>
Here's jquery.tagautocomplete.js
:
function bindAutoTagComplete(item, otherRootDomain)
{
function split( val ) {
return val.split( / \s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$(item).autocomplete({
source: function( request, response ) {
$.getJSON('http://jqueryui.com/demos/autocomplete/search.php', {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( " " );
return false;
}
});
}
What do you think may be causing this problem? I'm probably missing some closing parantheses/braces in the .js
file...
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在页面准备好后附加该事件。 #birds 当前运行时不存在。
像这样的东西
You need to attach that event after the page is ready. #birds doesn't exist when it runs currently.
Something like