将单个 Dojo 工具提示动态分配给多个节点

Dynamically assign single Dojo Tooltip to multiple Nodes

假设我们有一个带有节点列表(例如 div)的小部件。我们希望在鼠标悬停时显示 Dojo 工具提示。里面的元素是动态生成的,所以我们要通过编程来添加Tooltips。

策略是在postCreate期间首先定义Tooltip单次,然后将其传递给处理函数,后者将动态地将其添加到节点。

postCreate: function() {
  var _this = this;
  var fooTooltip = new Tooltip();

  this.own(on(this, '.elements-container-item', function(e) {
    lang.hitch(_this, 'onMouseOverHandler')(this, e, fooTooltip);
  });
}

Tooltip 动态分配给鼠标悬停的元素的正确方法是什么?

onMouseOverHandler: function(node, e, fooTooltip) {
  fooTooltip.set('connectId', [node]); // doesn't work
  fooTooltip.set('label', 'foo label'); // doesn't work as well
}

您可以为工具提示做这样的事情。 请记住,您需要在小部件定义中要求 dojo/query

postCreate: function() {
  var _this = this;
  var containerNode = this.domNode; // Assuming that the widget has a domNode

  var fooTooltip = new Tooltip({
     connectId: query('.list-container', containerNode ), // Search the Node starting at the containerNode.
     selector: '.list-container-item',
     getContent: function(matchedNode) {
        console.debug('this is a tooltip for ', matchedNode);
     }
  });

}