简体   繁体   English

即使我使用focus(),列表项也没有获得焦点

[英]List item is not getting focus even though I am using focus()

I need to display the list and focus first list item in the list when user selects key 'm' on the keyboard. 当用户在键盘上选择键“ m”时,我需要显示列表并在列表中聚焦第一个列表项。 I tried calling focus() on the list item but it is not getting focus or it is not showing any effect. 我尝试在列表项上调用focus(),但是它没有获得焦点或没有显示任何效果。 Below is the code. 下面是代码。

HTML: HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>VOD</title>
        <script src='js/index.js'>
        </script>
        <style>
            html, body {
                height:100%
            }

            #mid {
                display: flex;
                height: 100%;
                width: 100%;
                justify-content: stretch;
                flex-flow: row nowrap;
                z-index: 2;
            }

            #mid.hidden {
                display: none;
            }

            #mid1, #mid2 {
                display: flex;
                flex: 1;
                align-items: center;
            }

            #mid1 {
                justify-content: center;
                background-color: rgba(255, 255, 255, 0.5);
            }

            #mid2 {
                background-color: rgba(255, 255, 255, 0.6);
            }

            #ulid {
                list-style-type: none;
                width: 100%;
                margin: 0;
                border: 0;
                padding: 0;
            }

            .list-item {
                width: 100%;
                height: 150px;
                border-style: solid;
                border-width: 1px;
                display:flex;
                align-items: center;
                justify-content: flex-start;
            }

            li:focus, li:active {
                background-color: green;
            }
        </style>
    </head>
    <body>
        <video id='vid' src='textMotion.mp4' autoplay loop></video>
        <div id='mid' class='hidden'>
            <div id='mid1'>
                <h1>TEXT</h1>
            </div>
            <div id='mid2'>
                <ul id='ulid'></ul>
            </div>
        </div>
    </body>
</html>

JavaScript: JavaScript的:

function displayMenu() {
    var mid = document.getElementById('mid');

    if(mid.classList.contains('hidden') == false) {
        mid.classList.toggle("hidden");
        let ulid = document.getElementById('ulid');
        while(ulid.firstChild) {
            ulid.removeChild(ulid.firstChild);
        }
        return;
    }

    var ulid = document.getElementById('ulid');

    for(let index = 0; index < 3; index ++) {
        let lItem = document.createElement('li');
        lItem.classList.add('list-item');
        lItem.setAttribute('id', 'li' + index);

        var lineBreak = document.createElement('br');

        lItem.appendChild(document.createTextNode('TEXT'));
        lItem.appendChild(lineBreak);
        lItem.appendChild(document.createTextNode('TEXT'));
        ulid.appendChild(lItem);
    }

    mid.classList.toggle("hidden");
    document.getElementById('li0').focus();
}

function changeChannel(e) {
    console.log('received keyEvent : ' + e.keyCode);
    let keyCode = e.keyCode;

    if(keyCode == 77) {
        displayMenu();
    }
}

document.addEventListener('keydown', changeChannel);

Even though list is getting displayed when user presses 'm', list item is not getting focused. 即使当用户按下'm'时显示了列表,列表项也没有得到关注。

below is the jsfiddle link 下面是jsfiddle链接

https://jsfiddle.net/t75gojd7/ https://jsfiddle.net/t75gojd7/

Can anyone please help me to focus the list element. 谁能帮我把重点放在列表元素上。

It looks like adding a tabindex might work. 似乎添加一个tabindex可能有效。 Add this to the attributes. 将此添加到属性。

lItem.setAttribute('tabindex', index);

Got the idea from here . 这里得到了这个主意。 Worked for some people not all. 为某些人服务,而不是全部。

What is happening here is that the element you are trying to focus is a div which doesn't actually remain in focus after click unlike a button or a input text field. 这里发生的是您要聚焦的元素是一个div ,它与单击buttoninput文本字段不同,实际上在单击后并没有保持焦点。

You can observe this behavior by pressing down the pointer on the div and not releasing it. 您可以通过按下div上的指针而不释放它来观察这种行为。 Your CSS stylings will be applied. 您的CSS样式将被应用。

I would suggest instead on :focus CSS class, you use some other pseudo CSS class. 我建议在:focus CSS类上使用其他伪CSS类。 OR you can do event.preventDefault() on div click. 或者,您可以在div点击时执行event.preventDefault()

Quoting from MDN MDN报价

If you call HTMLElement.focus() from a mousedown event handler, you must call event.preventDefault() to keep the focus from leaving the HTMLElement. 如果从mousedown事件处理程序中调用HTMLElement.focus(),则必须调用event.preventDefault()来防止焦点离开HTMLElement。


Also check out this ans Is it possible to focus on a <div> using JavaScript focus() function? 还请检查这个问题。 是否可以使用JavaScript focus()函数将焦点放在<div>上? which suggests adding tabindex attribute. 建议添加tabindex属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在更新对象,即使变量不知道 express js 中的项目更新 - I am getting updated object even though the variable has no knowledge of item update in express js 为什么我在使用制表键和箭头键时无法专注于此菜单项? - Why am I not able to focus on this menu item when using tab and arrow keys? 即使我正在使用非缩小文件,ReactJS也会出现缩小错误 - ReactJS getting minified error even though I am using the non-minified files AngularJS:即使它们正确显示,为什么也要为此得到NaN? - AngularJS: Why am I getting NaN for this even though they display correctly? 如果我使用组件,如何将焦点转移到下一个输入? - How to change focus to the next input if I am using components? 即使元素存在,Vue.js 中的“焦点不是函数” - "focus is not a function" in Vue.js even though element exists 未捕获的类型错误:无法读取未定义的属性“焦点”,即使正在调用焦点 - Uncaught TypeError: Cannot read property 'focus' of undefined, even though focus is being called 如何使用 useRef 反应钩子关注列表项 - How to focus on a list item with useRef react hook jQuery将重点放在选择列表的所选项目上 - jQuery set focus on selected item of a select list 将默认焦点设置为网格列表中的第一项 - Set default focus on first item in grid list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM