我在 JQuery UI 自动完成方面遇到了一个奇怪的问题。发生的事情是当我尝试使用位于 JQUERY UI Autocomplete DEMOS 的代码时
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Autocomplete - Multiple values</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<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"
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
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( ", " );
focusCampo(this.id);
return false;
}
});
});
function focusCampo(id) {
var inputField = document.getElementById(id);
if (inputField != null && inputField.value.length != 0) {
if (inputField.createTextRange) {
var FieldRange = inputField.createTextRange();
FieldRange.moveStart('character', inputField.value.length);
FieldRange.collapse();
FieldRange.select();
} else if (inputField.selectionStart || inputField.selectionStart == '0') {
var elemLen = inputField.value.length;
inputField.selectionStart = elemLen;
inputField.selectionEnd = elemLen;
inputField.focus();
}
} else {
inputField.focus();
}
}
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tag programming languages: </label>
<input id="tags" size="50" />
</div>
</body>
</html>
使用我从 Stackoverflow 获得的附加脚本。我不记得确切的链接。我能够解决我的光标和文本对齐问题。但是在 Chrome 上它不起作用!在 chrome 上它看起来像 这就是它在 CHROME 上的样子 http://img405.imageshack.us/img405/7144/uiissues.png 请有人帮我找出这段代码中的问题吗?该代码适用于其他浏览器。请尝试代码..它已完全编写。只需复制粘贴并运行。它在 IE9,FF 中运行良好。确切的问题是光标位置不同,但文本框内容在 chrome 中未正确显示 .. 即它没有向前移动光标实际所在的位置。请尝试一下并告诉我。抱歉英语不好..无法正确解释问题..词汇量不足!