【问题标题】:TypeError: blur is not a functionTypeError:模糊不是函数
【发布时间】:2019-05-01 02:22:01
【问题描述】:

在我的 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()之前是否应该检查它是否存在?

【问题讨论】:

  • 您是在页面加载之前还是之后加载 Javascript 代码?
  • ui = An Object with label and value properties for the selected option. 不是实际输入,你可以试试event.currentTarget.blur()
  • @merkur0 我把所有东西都包裹在了$(document).ready();
  • 你确认ui是一个元素吗?
  • @Lewis 它可能不是一个元素。有什么好的验证方法? console.log(ui);?

标签: javascript jquery jquery-ui blur


【解决方案1】:

ui 是自动完成附加到的 DOM 元素,而不是 jQuery 对象。您需要调用jQuery() 才能调用.blur() 之类的方法。所以改变

ui.blur();

到

$(ui).blur();

这在整个 jQuery 中很常见——回调通常接收 DOM 元素而不是 jQuery 包装器对象。例如。当你使用.each()时,回调函数的第二个参数是DOM元素。

【讨论】:

  • 也可以使用$(this).blur();。
  • 在 React Native 中没有 jquery 是否可以做到这一点?我正在使用这个fieldRef.current?.blur();,我收到TypeError: _fieldRef$current.blur is not a function 的错误。这是我最初的问题:stackoverflow.com/questions/63757905/…@Twisty
  • 请参阅code.tutsplus.com/tutorials/… 将 jQuery 转换为 vanilla JS。
猜你喜欢
  • 2020-09-06
  • 2020-06-29
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 2018-05-24
  • 2020-03-03
  • 2021-06-05
相关资源
最近更新 更多