如何关注div?

use*_*399 5 html jquery

请不要向我扔石头,因为我是js和jquery的新手.是否有可能专注于div?我只是想在点击div时处理事件,或者当我们点击div之外时处理事件.像这样的东西:

HTML:

<div id="focusedDiv"></div>
Run Code Online (Sandbox Code Playgroud)

JS:

    $("#focusedDiv").focus(function () {
        ....
    });

    $("#focusedDiv").focusout(function () {
        ....        
    });
Run Code Online (Sandbox Code Playgroud)

提前致谢!

A. *_*lff 27

您必须设置tabindex属性:

http://jsfiddle.net/QAkGV/

<div id="focusedDiv" tabindex="-1"></div>
Run Code Online (Sandbox Code Playgroud)

要么:

$("#focusedDiv").attr('tabindex',-1).focus(function () {
        ....
    });
Run Code Online (Sandbox Code Playgroud)

对于样式,您也可以设置大纲CSS属性:

$("#focusedDiv").css('outline',0).attr('tabindex',-1).focus(function () {
        ....
    });
Run Code Online (Sandbox Code Playgroud)