3

我正在开发 Dojo 1.8 版。我设计了一个自定义小部件,如下所示。它是一个片段

<div>
	<div>
		 <input 
			 id ="NZ1",
			 data-dojo-attch-point = "NZ1"
			 data-dojo-attch-type = "ecm.widget.ValidationTextBox"
			 data-dojo-attch-event = "onBlur : makeAllSmall"
		 />
	</div>
	<div>
		 <input 
			 id ="NZ2",
			 data-dojo-attch-point = "NZ2"
			 data-dojo-attch-type = "ecm.widget.ValidationTextBox"
			 data-dojo-attch-event = "onBlur: makeAllSmall"
		 />
	</div>
</div> 

这是事件处理程序

makeAllSmall : function(evt){  
	var currVal=evt.target.value;
	currVal = currVal.toLowerCase();
	/**Some Other business logic on currVal **/
}

这个evt总是以 undefined 的形式出现。我对 Dojo 很陌生。我在 HTML 方面遗漏了什么吗?我试图改变 HTML 如下但不是运气

		 <input 
			 id ="NZ2",
			 data-dojo-attch-point = "NZ2"
			 data-dojo-attch-type = "ecm.widget.ValidationTextBox"
			 data-dojo-attch-event = "onBlur : makeAllSmall"
			 data-dojo-args="e"
		 />

4

2 回答 2

1

首先,“onBlurr”方法中是否有错字?我看到有一个额外的'r'。不应该是“onBlur”吗?

如果您查看 onBlur 事件的 DOJO API 文档,它不会像您期望的那样传递事件对象

onBlur()
Defined by: dijit/_FocusMixin

Called when the widget stops being "active" because focus moved to something outside of it, or the user clicked somewhere outside of it, or the widget was hidden.
Examples
Example 1
var btn = new Button();
// when /my/topic is published, this button changes its label to
// be the parameter of the topic.
btn.subscribe("/my/topic", function(v){
this.set("label", v);
});

接下来,在您的事件处理程序中,您尝试将文本更改为小写,这可以像

makeAllSmall : function(){  
    var currVal=this.get("value");
    currVal = currVal.toLowerCase();
    /**Some Other business logic on currVal **/
}

在没有事件处理程序的情况下执行此操作的另一种方法是强制ValidationTextBox使用构造参数将所有内容转换为小写,例如

<input 
             id ="NZ2",
             data-dojo-attach-point = "NZ2"
             data-dojo-attach-type = "ecm.widget.ValidationTextBox"
            data-dojo-props='lowercase:true'
             data-dojo-attach-event = "onBlurr : makeAllSmall"
         />

请注意,我已添加data-dojo-props='lowercase:true'

希望这可以帮助。

于 2017-10-09T16:24:45.417 回答
0

您应该能够通过以下方式将 DOM 事件附加到您的自定义小部件:

  • data-dojo-attach-event在标记中使用数据属性。
  • 并使用_AttachMixin传递你的回调函数。

例子:


<div id="somenode"><span data-dojo-attach-point="anattachpoint"
     data-dojo-attach-event="click: clicked">Click me</span></div>

var MyDijit = declare([ _WidgetBase, _AttachMixin ], {
    // .. declaration goes here ..
    clicked: function(e) {
        // handle event
    }
});
// instantiate the dijit instance, which will attach to the 'somenode' div.
var mydijit = new MyDijit({}, dom.byId('somenode'));
mydijit.startup();
于 2017-10-11T19:10:27.430 回答