当elem出现时,自动对焦不能正常工作。

时间:2021-12-03 20:38:32

I'm not getting autofocus on the input when it shows up. The autofocus property works intermitently on Chrome. On IE11... well it does not even try to add it.

当它出现时,我不会自动关注输入。自动对焦特性在Chrome上断断续续地工作。IE11…它甚至没有尝试添加它。

Do I need to validate the existence of the input element on the DOM before apply the autofocus with jQuery?

在将autofocus应用于jQuery之前,是否需要验证DOM中输入元素的存在?

HTML5

HTML5

<input type="text" class="form-control" autofocus placeholder="Filter">

JS

JS

   function iboxHistorySearch() {
        var header = $('.client-history .ibox-title_replace'),
            searchbar = $('.client-history .client-history-searchbar'),
            closeBtn = $('.client-history .btn-dismiss');

        header.on('click', function (e) {
            e.preventDefault();
            if (searchbar.hasClass('hidden')) {
                searchbar.removeClass('hidden')find('form-control:first').focus();
                $(this).addClass('hidden');
            }
        });

        closeBtn.on('click', function (e) {
            e.preventDefault();
            if (header.hasClass('hidden')) {
                header.removeClass('hidden');
                searchbar.addClass('hidden');
            }
        });
    }

    iboxHistorySearch();

UPDATE

更新

searchbar.removeClass('hidden')find('form-control:first').focus();

1 个解决方案

#1


1  

From the (unedited) question:

(未经编辑)的问题:

var searchbar = $('.client-history .client-history-searchbar')

if (searchbar.hasClass('hidden')) {
    searchbar.removeClass('hidden').focus();

make sure that searchbar is an input control that can receive focus. If not, add a .find to get an input, either the first or a specific element, eg:

确保searchbar是一个可以接收焦点的输入控件。如果没有,添加一个。find获得一个输入,或者是第一个或一个特定的元素,例如:

    searchbar.removeClass('hidden')
             .find('.form-control:first')
             .focus();

or

    searchbar.removeClass('hidden')
             .find('input:first')
             .focus();

(but with input you may also need to add checkbox/select etc, eg)

(但有了输入,你可能还需要添加复选框/选择等)

    searchbar.removeClass('hidden')
             .find('input,select,textarea')
             .first()
             .focus();

#1


1  

From the (unedited) question:

(未经编辑)的问题:

var searchbar = $('.client-history .client-history-searchbar')

if (searchbar.hasClass('hidden')) {
    searchbar.removeClass('hidden').focus();

make sure that searchbar is an input control that can receive focus. If not, add a .find to get an input, either the first or a specific element, eg:

确保searchbar是一个可以接收焦点的输入控件。如果没有,添加一个。find获得一个输入,或者是第一个或一个特定的元素,例如:

    searchbar.removeClass('hidden')
             .find('.form-control:first')
             .focus();

or

    searchbar.removeClass('hidden')
             .find('input:first')
             .focus();

(but with input you may also need to add checkbox/select etc, eg)

(但有了输入,你可能还需要添加复选框/选择等)

    searchbar.removeClass('hidden')
             .find('input,select,textarea')
             .first()
             .focus();