Dojo,如何在DIV上执行onclick事件 [英] Dojo, how to do onclick event on a DIV
问题描述
互联网上有一个淡出的样本。
http:/ /docs.dojocampus.org/dojo/fadeOut?t=tundra
There was a fade out sample in the internet.. http://docs.dojocampus.org/dojo/fadeOut?t=tundra
但我想做一些不同的事情..
i要人直接点击在文本上,文本将淡出。
but i want to do something different.. i want people directly click on the text then the text will fade out.
在我的代码中有一个div包装文本
in my code there is a div wrap the text
<div id='parentNode'>
<div id='textDiv' onClick='whenClickAnyWhereWithinThisDiv_performFadeOut()'>
<div id='iconDiv'/>
<div id='messageDiv'/>
</div>
<div>
代码如下所示,我想要的是,当人们点击textDiv中的任何地方,
那么整个textDiv将会消失。嗯.....为什么我的代码不工作?
Code as show below, what i want is, when people click anywhere within the textDiv, then the whole textDiv will fade away..hmm.....why my code doesn`t work???
function whenClickAnyWhereWithinThisDiv_performFadeOut () {
...
...
dojo.connect(dijit.byId('textDiv'), "onClick", fadeOutAndRemove(parentNode, textDiv));
}
function fadeOutAndRemove (parent, currentDiv) {
// just assume i can get the parent Node, and the current div, which will be textDiv
var objectId = currentDiv.getAttribute('id');
dojo.style(objectId, "opacity", "1");
var fadeArgs = {
node: objectId,
duration: 2000
};
dojo.fadeOut(fadeArgs).play();
setTimeout(function() { parent.removeChild(currentDiv);}, 2000);
}
推荐答案
如果我明白你是我想这样做可以实现:
If I understand what you are trying to do, I think you can accomplish it with this:
HTML
<div id='parentNode'>
<div id='textDiv'>
<div id='iconDiv'>this is icon div</div>
<div id='messageDiv'>this is message div</div>
</div>
<div>
JavaScript
// do it when the DOM is loaded
dojo.addOnLoad( function() {
// attach on click to id="textDiv"
dojo.query('#textDiv').onclick( function(evt) {
// 'this' is now the element clicked on (e.g. id="textDiv")
var el = this;
// set opacity
dojo.style(this, "opacity","1");
// do fade out
dojo.fadeOut({
node: this,
duration: 2000,
// pass in an onEnd function ref that'll get run at end of animation
onEnd: function() {
// get rid of it
dojo.query(el).orphan()
}
}).play()
});
});
点击将起泡,所以它将被 textDiv抓住。 / code>
The click will bubble up so it'll be caught by textDiv.
以下是一些有用的链接:
Here are some helpful links:
- <一个href =http://docs.dojocampus.org/quickstart/Animation =nofollow noreferrer> Dojo动画快速入门
- dojo.byId vs. dijit.byId
- Dojo Animation quickstart
- dojo.byId vs. dijit.byId
如果我误解了你的问题,我会更新我的答案。希望这有帮助。
Let me know if I misunderstood your question and I'll update my answer. Hope this helps.
这篇关于Dojo,如何在DIV上执行onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!