来自 http://jqueryui.com/demos/autocomplete/ 的“焦点”文档状态:
focusType:autocompletefocus Before focus is moved to an item (not selecting), ui.item refers to the focused item. The default action of focus is to replace the text field's value with the value of the focused item, though only if the focus event was triggered by a keyboard interaction. Canceling this event prevents the value from being updated, but does not prevent the menu item from being focused.
Code examples
Supply a callback function to handle the focus event as an init option. $( ".selector" ).autocomplete({ focus: function(event, ui) { ... } }); Bind to the focus event by type: autocompletefocus. $( ".selector" ).bind( "autocompletefocus", function(event, ui) { ... });
使用下面的代码在所有自动完成元素上设置一个名为“mytag”且值为“tester”的属性,即使我尚未选择这些元素。为什么属性“focus”不只在其中一个下拉菜单获得焦点时添加,而是在页面加载时添加?
$("#myDiv").autocomplete({
source: availableTags,
focus: function(event, ui) {
$(".ui-autocomplete li").attr("mytag", "tester");
}
});
最佳答案
当用鼠标悬停在建议的完成上或使用箭头键选择一个时。这是一个正常的“焦点”,应用于自动完成框。
自己尝试一下:http://jsfiddle.net/qk9gv/ !
关于javascript - 'focus'什么时候调用 'autocomplete',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12391627/