Dojo:如何在Dojo中查找小部件是否具有焦点

5jvtdoz2  于 2022-12-16  发布在  Dojo
关注(0)|答案(4)|浏览(172)

如何确定我定制小部件是否在Dojo中具有焦点
我有dojo编辑器我想知道如果编辑器已经焦点或没有?

9q78igpj

9q78igpj1#

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

来自DOJO文档

跟踪活动微件
在任何时间点,都有一组“活动的”或“聚焦的”小部件(因为没有更好的词),表示当前聚焦的小部件和该小部件的祖先。TextBox -〉Form)或逻辑父子关系(例如:工具提示对话框-〉下拉按钮)。
例如,如果焦点位于由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(", "));
});
});

字符串

q5lcpyga

q5lcpyga2#

标题中的问题与描述中的问题有不同的答案。
有两种方法可以解决标题中的问题,一种是使用dojo的focusUtil(“dijit/focus”),两种方法都给予您找到使用它的小部件和dijit的注册表(“dijit/registry”)
1.一月一日:给你当前有焦点的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)

1.focusUtil.activeStack:给你一个有焦点的小部件数组(父到子).所以数组中的最后一个项目是有焦点的直接小部件.索引值是小部件id,所以你应该通过下面的代码得到小部件

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

现在,如果你想知道当前聚焦的小部件是否是某个特定的小部件,这取决于你从那个特定的小部件中得到了什么:
1.小工具本身:喜欢这返回值的上述范例.现在你必须比较如果这是相同的事情.你不能比较两个小部件对象使用这==操作符.你能比较他们的id象这:

myWidget.id == focusedWidget.id

1.**widget 's id:**这样您就可以轻松地从focusUtil获取当前节点的id,并将其与您拥有的id进行比较,如以下所示:

myWidgetId == focusedWidgetId

参考文献:
http://dojotoolkit.org/reference-guide/1.9/dijit/focus.html
http://dojotoolkit.org/reference-guide/1.9/dijit/registry.html

qni6mghb

qni6mghb3#

需要([“dijit/焦点”],函数(focusUtil){
变量活动元素=焦点实用程序.当前节点;//如果没有焦点元素,则返回null
);
检查下面的网址在这里你可以看到一些例子http://dojotoolkit.org/reference-guide/1.8/dijit/focus.html#dijit-focus

a8jjtwal

a8jjtwal4#

a)对于**dojo 1.6:**call dijit.getFocus() .这将返回一个包含当前焦点dom节点的对象,以及其他内容(选定文本等).要获得相应的小部件,只需执行以下操作:

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

这是dijit.getFocus()的完整参考,来自源代码:

// 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)对于**dojo 1.7及更高版本,**使用dijit/focus

require([ "dijit/focus" ], function(focusUtil) {
    var activeElement = focusUtil.curNode; // returns null if there is no focused element
});

相关问题