TypeError: blur is not a function
TypeError: blur is not a function
在我的 JQuery UI 自动完成功能中,我使用 .blur()
在做出选择时关闭 IOS 键盘:
// Purpose: Instantiate Autocomplete
function autocomplete() {
searchInput.autocomplete({
source: autocompleteCourts,
minLength: 3,
select: function (event, ui) {
location.hash = "trigger-header";
isEFile(event, ui);
// Close keyboard on IOS when an option is selected
ui.blur();
}, open: function (event, ui) {
$("li.ui-menu-item:odd").addClass("ui-menu-item-alternate");
$(".ui-menu-item-alternate").css("background-color", "#f2f4f7");
$("ul.ui-menu").addClass("mt-2 w-auto");
$("ul.ui-menu").css("z-index", 0);
}
});
浏览器抛出错误 Uncaught TypeError: ui.blur is not a function
。
我是否应该在调用函数 blur()
之前检查它是否存在?
ui
是自动完成附加到的 DOM 元素,而不是 jQuery 对象。您需要调用 jQuery()
才能调用 .blur()
等方法。所以改变
ui.blur();
至
$(ui).blur();
这在整个 jQuery 中很常见——回调通常接收 DOM 元素而不是 jQuery 包装器对象。例如。当您使用 .each()
时,回调函数的第二个参数是 DOM 元素。
在我的 JQuery UI 自动完成功能中,我使用 .blur()
在做出选择时关闭 IOS 键盘:
// Purpose: Instantiate Autocomplete
function autocomplete() {
searchInput.autocomplete({
source: autocompleteCourts,
minLength: 3,
select: function (event, ui) {
location.hash = "trigger-header";
isEFile(event, ui);
// Close keyboard on IOS when an option is selected
ui.blur();
}, open: function (event, ui) {
$("li.ui-menu-item:odd").addClass("ui-menu-item-alternate");
$(".ui-menu-item-alternate").css("background-color", "#f2f4f7");
$("ul.ui-menu").addClass("mt-2 w-auto");
$("ul.ui-menu").css("z-index", 0);
}
});
浏览器抛出错误 Uncaught TypeError: ui.blur is not a function
。
我是否应该在调用函数 blur()
之前检查它是否存在?
ui
是自动完成附加到的 DOM 元素,而不是 jQuery 对象。您需要调用 jQuery()
才能调用 .blur()
等方法。所以改变
ui.blur();
至
$(ui).blur();
这在整个 jQuery 中很常见——回调通常接收 DOM 元素而不是 jQuery 包装器对象。例如。当您使用 .each()
时,回调函数的第二个参数是 DOM 元素。