我将“this”指向 DOM 元素(div
或 form
)。我想对该元素使用 dojo 函数。我将如何做。
就像在 jQuery 中一样,我们执行 $(this).append()
....
有没有类似的东西
dojo.foo(this).connect()
或
dojo.connect(dojo.foo(this),"some", thing);
最佳答案
在 Dojo 中,您比在 jQuery 中更接近 JavaScript(原始金属)。
所以在 Dojo 中,你只需要做:
dojo.connect(this, ...);
您不必使用类对象(如 jQuery 的 $)“包装”DOM 元素来使用这些功能。 Dojo 中的很多功能并没有暴露为类对象的原型(prototype)属性,而是暴露为 dojo.xxx 命名空间系统下的简单函数。
例如(假设“this”指向一个DOM节点):
dojo.connect(this, "onclick", foo, "bar"); // Connects a handler to the Click event on the DOM node, with the context (i.e. this) set to the object foo
dojo.attr(this, "href", "http://www.hello.com"); // Sets an attribute on the DOM node
dojo.style(this, "display", "none"); // Sets the DOM node's style
dojo.addClass(this, "hello"); // Adds a class to the DOM node
alert(this.parentNode); // You work the DOM nodes with raw JavaScript
dojo.empty(this); // Empty all children in the DOM node
dojo.destroy(this); // Destroy the DOM node and its children
dojo.place(this, someOtherNode); // Put a DOM node as a child inside another node
循环构造:
dojo.forEach(array, ...); // Instead of array.each(...) as in jQuery style
如果你想遍历一个节点列表,它实际上看起来像 jQuery:
dojo.query('query string').filter(...).forEach(...);
阅读文档了解更多详情。
关于javascript - 获取 DOM 元素的 dojo 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6404136/