using :focus pseudo class on li or other element else than a,input,button, etc

那年仲夏 提交于 2019-11-27 07:39:44

问题


Is it possible to select an element that is focused, like a:focus or input:focus, but on the other element, such as div, li, span or else?

If it's not possible, how can it be substituted with a tag? I am trying to modify height of a li element when it's focused, tried with a, but the height won't change.


Requested Info :

"Can you also explain how do you put focus on LI?"

Via css selector li:focus. See https://jsfiddle.net/s4nji/Rjqy5/.


回答1:


That's simple: just add a tabindex attribute to the elements which normally don't receive focus.




回答2:


You can't focus list items. Not even using a script. I've tried doing $("li:first").focus() but it doesn't do anything. CSS doesn't get set as if it was focussed. This simply means that you either can't focus list items or that :focus pseudo classes don't work on list items.

Put anchors inside list items

<ul>
    <li><a href="#">...</a></li>
    <li><a href="#">...</a></li>
    <li><a href="#">...</a></li>
    ...
</ul>

This will seemingly focus your list item but it will actually focus anchor tags.



来源:https://stackoverflow.com/questions/8614862/using-focus-pseudo-class-on-li-or-other-element-else-than-a-input-button-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!