2

如何确定我的自定义小部件是否在Dojo中具有焦点

我有道场编辑器,我想知道编辑器是否已经集中注意力?

4

4 回答 4

3

您可以使用模块 dijit/focus 找出焦点

来自 DOJO 文档

跟踪活动小部件

在任何时间点都有一组(因为没有更好的词)“活动”或“聚焦”小部件,表示当前聚焦的小部件和该小部件的祖先。“祖先”可以表示 DOM 祖先(例如:TextBox –> Form),或逻辑父子关系(例如:TooltipDialog –> DropDownButton)。

例如,如果焦点位于由 DropDownButton 触发的 TooltipDialog 内 TabContainer 内的 TextBox 上,则堆栈将为 TextBox –> ContentPane –> TabContainer –> TooltipDialog –> DropDownButton。

activeStack[] 参数表示这组小部件,应用可以通过以下方式监控对 activeStack[] 的更改:

require([ "dijit/focus" ], function(focusUtil){
 focusUtil.watch("activeStack", function(name, oldValue, newValue){
  console.log("Focused widget + ancestors: ", newValue.join(", "));
});
});
于 2013-05-18T10:38:22.673 回答
3

标题中的问题与描述中的问题有不同的答案。

通过使用 dojo 的focusUtil ("dijit/focus"),有两种方法可以解决标题中的问题。这两种方式都给你一些东西,你可以找到使用它的小部件和 dijit 的注册表(“dijit/registry”)

  1. focusUtil.curNode: 为您提供当前具有焦点的 DOM 节点。下面的函数,您可以获得小部件参考。

    function getWidgetByNode(node){
        var result;
        while (!result && node){
            result = registry.byNode(node);
            if (node.parentElement)
                node = node.parentElement;
            else
                node = null;
        }
        return result;
    }
    var focusedWidget = getWidgetByNode(focusUtil.curNode)
    
  2. focusUtil.activeStack: 为您提供具有焦点的小部件数组(从父到子)。所以数组中的最后一项是具有焦点的直接小部件。index 值是小部件 id,因此您应该通过以下代码获取小部件

    var focusedWidgetId = focusUtil.activeStack[focusUtil.activeStack.length-1];
    var focusedWidget = registry.byId(focusedWidgetId);
    

现在,如果您想知道当前聚焦的小部件是否是某个特定的小部件,这取决于您从该特定小部件中获得的内容:

  1. widget itself: like the return values of above samples. now you have to compare if these are the same thing. you can not compare two widget objects using the == operator. you could compare their ids like this:

    myWidget.id == focusedWidget.id
    
  2. widget's id: this way you just easily get the id of the current node from focusUtil and compare it with the id you have liek this:

    myWidgetId == focusedWidgetId
    

references:
http://dojotoolkit.org/reference-guide/1.9/dijit/focus.html
http://dojotoolkit.org/reference-guide/1.9/dijit/registry.html

于 2013-12-17T15:34:05.593 回答
0

require([ "dijit/focus" ], function(focusUtil){
var activeElement = focusUtil.curNode; // 如果没有焦点元素,则返回 null
});

检查打击网址在这里你可以看到一些例子 http://dojotoolkit.org/reference-guide/1.8/dijit/focus.html#dijit-focus

于 2013-10-01T08:43:40.133 回答
0

a) For dojo 1.6: call dijit.getFocus(). This will return an object containing the currently focused dom node, among other things (selected text, etc.). To get the corresponding widget, simply do:

var activeElement = dijit.getEnclosingWidget(dijit.getFocus().node);

This is the full reference for dijit.getFocus(), from the source code:

// summary:
//      Called as getFocus(), this returns an Object showing the current focus
//      and selected text.
//
//      Called as getFocus(widget), where widget is a (widget representing) a button
//      that was just pressed, it returns where focus was before that button
//      was pressed.   (Pressing the button may have either shifted focus to the button,
//      or removed focus altogether.)   In this case the selected text is not returned,
//      since it can't be accurately determined.
//
// menu: dijit._Widget or {domNode: DomNode} structure
//      The button that was just pressed.  If focus has disappeared or moved
//      to this button, returns the previous focus.  In this case the bookmark
//      information is already lost, and null is returned.
//
// openedForWindow:
//      iframe in which menu was opened
//
// returns:
//      A handle to restore focus/selection, to be passed to `dijit.focus`.

b) For dojo 1.7 and up, use dijit/focus:

require([ "dijit/focus" ], function(focusUtil) {
    var activeElement = focusUtil.curNode; // returns null if there is no focused element
});
于 2018-08-09T01:15:02.453 回答