admin管理员组文章数量:1410737
I have a list, and within this list im trying to call a function when keyisdown/onfocus/etc? This wont trigger the function:
<li onkeydown="myFunction()"></li>
The normal onclick works fine(calling the function when the "click is going up") and will trigger the function:
<li onclick="andrafarg()"></li>
So my question is: How can i call this function when the list is clicked immediately down?
I have a list, and within this list im trying to call a function when keyisdown/onfocus/etc? This wont trigger the function:
<li onkeydown="myFunction()"></li>
The normal onclick works fine(calling the function when the "click is going up") and will trigger the function:
<li onclick="andrafarg()"></li>
So my question is: How can i call this function when the list is clicked immediately down?
Share Improve this question asked May 8, 2015 at 14:35 MattiasMattias 3,1995 gold badges32 silver badges49 bronze badges1 Answer
Reset to default 6From the jQuery docs:
The keydown event is sent to an element when the user first presses a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.
So as li
elements can't get focused, the keydown
-event will never be triggered.
For a reference for focusable elements, you might have a look at this answer.
But because of the event-bubbling, an input
-field inside the list will trigger the keydown
-event, if the event-listener is registered on the input itself or a parent-element.
Example:
HTML
<ul>
<li><input type="text" /></li>
</ul>
Javascript
$('ul > li').on('keydown', function(){
alert('Event triggered');
});
Demo
本文标签: javascriptHow to call onkeydownonfocus to a list tagStack Overflow
版权声明:本文标题:javascript - How to call onkeydownonfocus to a list tag? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744844923a2628142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论