简体   繁体   English

反正有没有让 div 模糊或聚焦?

[英]Is there anyway to make a div blur or focus?

If i give a div a focus/blur event handler, when will the handler (if ever) run?如果我给 div 一个焦点/模糊事件处理程序,该处理程序(如果有的话)何时运行?

If you give the div a tabindex attribute, it will be able to accept focus:如果你给div一个tabindex属性,它将能够接受焦点:

<div id="example" tabindex="0">Random content</div>

You can then attach focus and blur event handlers as you normally would.然后,您可以像往常一样附加focusblur事件处理程序。 For example:例如:

document.getElementById("example").onfocus = function() {
    console.log("focused");
}

Focus will be given to the div when you click on it, and blur will trigger when you click on any other element.单击div时将获得焦点,单击任何其他元素时会触发blur

If you give a div element a tabindex attribute, then it will be focusable (in browsers that support that part of the HTML 5 draft).如果你给一个 div 元素一个 tabindex 属性,那么它将是可聚焦的(在支持 HTML 5 草案的那部分的浏览器中)。 Theoretically it will then fire focus and blur events.理论上它会触发焦点和模糊事件。

Generally speaking, you shouldn't do that though.一般来说,你不应该这样做。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM