如何检查度过一个div在jQuery的集中与否? [英] how to check weather a div is focused or not in jquery?

查看:114
本文介绍了如何检查度过一个div在jQuery的集中与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我看来使用剑道格,我想在网格中创建新行后pressing回车键。我可以通过编写以下男女同校的做到这一点:

I'm trying to use kendo grid in my view, I want to create new row in the grid after pressing enter key. I can do this by writing following coed:

<div id="GridContainer">
<div id="grid"></div>
</div>

$(document.body).keypress(function (e) {
    if (e.keyCode == 13) {
        var grid = $("#grid").data("kendoGrid");
        grid.addRow();
}
});

问题是,页面,每当我preSS进入,它创造新的一行。但我想,只有当电网集中。我该怎么办呢?我试图将焦点放到股利,包含网格,但没有运气。我跳过了code生成可读性剑道网格。谢谢你。

The problem is, on the page, whenever I press enter, its creating new row. But I want that only when the grid is focused. How can I do that? I tried to apply focus to the div, that contains the grid, but no luck. I skipped the code to generate the kendo grid for readability. Thanks.

推荐答案

试试这个,

HTML

<div id="grid" tabindex="0"></div>

<div id="grid" contenteditable="true"></div>

SCRIPT

$(document.body).keypress(function (e) {
    if (e.keyCode == 13 && $("#grid").is(':focus')) {
        var grid = $("#grid").data("kendoGrid");
        grid.addRow();
    }
});

读<一个href=\"http://stackoverflow.com/questions/148361/how-can-i-give-keyboard-focus-to-a-div-and-attach-keyboard-event-handlers-to-it\">How ,我可以给键盘焦点到一个DIV和附加键盘事件处理器呢?<​​/A>

<一href=\"http://stackoverflow.com/questions/5594431/jquery-focus-not-working-without-tabindex-attribute-of-div\">Jquery 。重点()不工作无格 tabindex属性

Read How can I give keyboard focus to a DIV and attach keyboard event handlers to it? And Jquery .focus() not working without tabindex attribute of div

更新

焦点 DIV 添加一行之后再次一样,

$(document.body).keypress(function (e) {
    if (e.keyCode == 13 && $("#grid").is(':focus')) {
        var grid = $("#grid").data("kendoGrid");
        grid.addRow();
        $("#grid").focus();
    }
});

这篇关于如何检查度过一个div在jQuery的集中与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆