How to add a new method to Dojo?
Could you please tell how to add a new method to Dojo?
It works for me, but only for NodeList:
dojo.extend(dojo.NodeList, {
foo: function() {
alert(1)
}
});
dojo.byId("foo").foo();
But, i need for Element:
dojo.开发者_如何学JAVAbyId("id").myMethod();
The dojo.byId
function is just an alias for document.getElementById
. Therefore it returns a vanilla domNode, and what you suggest would be adding a new method to Element, not Dojo.
Dojo intentionally doesn't change Element, because it's considered bad practice to do so by some (it may collide with other frameworks, for example).
If you want to do it, you can add functions to Element's prototype:
Element.prototype.myMethod = function() {
alert("My content is: " + this.innerHTML);
};
Then you can do:
dojo.byId("id").myMethod();
精彩评论