jquery - 如何聚焦div?

标签 jquery html

<分区>

请不要向我扔石头,因为我是 js 和 jquery 的新手。 是否可以聚焦一个div? 我只想在单击 div 或聚焦时以及我们在 div 外部单击时处理事件。类似的东西:

HTML:

<div id="focusedDiv"></div>

JS:

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

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

提前致谢!

最佳答案

您必须设置 tabindex 属性:

http://jsfiddle.net/QAkGV/

<div id="focusedDiv" tabindex="-1"></div>

或者:

$("#focusedDiv").attr('tabindex',-1).focus(function () {
        ....
    });

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

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

关于jquery - 如何聚焦div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17042431/

相关文章:

javascript - dynatree - 如何滚动到事件节点?

javascript - 制作一个防止存储在浏览器历史记录中的网页

javascript - 使用 JavaScript 从 JSON 获取图像并放入 html div 中

javascript - jqplot 在 IE7 中运行不佳

jquery - 根据类名显示 block

javascript - 如何调整 Vimeo 播放器的大小?

javascript - jQuery 验证验证错误字段

html - 如何在 CSS 中将元素 float 到中心?

html - IE9 : wrong rendering of Arial (bolder)

html - 两个宽度相同的div不相同