jQuery .focus() 无法使用 ng-repeat (AngularJS) 处理动态生成的列表项

jQuery .focus() not working with a dynamically generated list item using ng-repeat (AngularJS)

本文关键字:动态 列表 处理 ng-repeat focus jQuery AngularJS      更新时间:2023-09-26

我正在使网站可通过键盘访问,并且我正在尝试使下拉菜单键盘可访问。带有 id= list-0 的元素是我试图关注的。

该 HTML 是:

<dl class="office-type-dropdown"> 
                        <dt tabindex=0 href="#" id='emerald-button' role="button" aria-haspopup="true" ng-keydown="b_onKeyDown($event)">
                            <a href="javascript:void(0);">    
                                <p class="multiSel">
                                    <span ng-repeat="obj in selectedItems">{{obj}}</span>
                                </p>  
                            </a>
                        </dt>
                        <dd>
                            <div class="mutliSelect">
                                <ul role="menu" id="office-select-menu">
                                    <li ng-repeat="obj in officeTypes" id="list-{{$index}}" aria-posinset="{{$index+1}}" ng-keydown="m_onKeyDown($event)">
                                        <label for="checkbox-{{index}}">
                                            <input tabindex="-1" role="menuitem" id="checkbox-{{$index}}" type="checkbox"  class="{{obj.class}}" ng-change='onCompleteFilter(obj)' ng-model="obj.checked">
                                            {{obj.text}}
                                        </label>
                                    </li>
                                </ul>
                            </div>
                        </dd>
                    </dl>

我试图使列表项成为焦点的 jQuery 是:

$scope.b_onKeyDown = function ($event) {
    var key = event.which;
    if(key == 13){
        $('#office-select-menu').toggle();
            setTimeout(function(){
                $('#emerald-button').blur();
                $('#list-0').attr("autofocus","autofocus");
                $('#list-0').focus();
            }, 100);
    }
}

在搜索其他问题后,我添加了 SetTimeOut 和自动对焦,但它们也不起作用。然而,焦点是转到html标签,当我做document.activeElement.innerHTML时,它会吐出整个HTML。

任何建议都会很棒。谢谢

jquery ID 中不能包含"-",除非你转义它。

http://api.jquery.com/category/selectors/