Mootools“事件”仅在第一次“单击”时起作用,之后停止工作。为什么?
Mootools Events
仅在第一次点击
时起作用,之后停止工作。
希望有人对此有疑问:http://jsfiddle.net/3j3Ws/
CSS
ul li,li.selected div{
width:22px;
height:22px;
display:block;
background:#000;
color:#fff;
text-align:center;
border-radius:3px;
}
ul#list{
display:none;
opacity:0;
float:left;
}
HTML
<ul id="option">
<li class="selected" id="a">a</li>
<ul id="list">
<li id="b">b</li>
<li id="c">c</li>
<li id="d">d</li>
</ul>
</ul>
Mootool JavaScript
window.addEvent('domready', function(){
var x = '<div>v</div>';
$$('ul#option li.selected').set('html',x);
var opt = $$('ul#option li.selected div');
var d = opt.getStyle('display');
var l = document.id('list');
var list = opt.set('morph').addEvents({
click:function(){
l.store('timerA',(function(){
l.morph({
'display':'block',
'opacity':1
});
$$('ul#option li.selected').setStyle('background-color','#fff');
$$('ul#option li.selected div').destroy();
}).delay(10,this));//$clear(this.retrieve('timerA'));
}
}
);
l.set('morph').addEvents({
mouseleave:function(el){
this.store('timerB',(function(){
this.morph({
'display':d,
'opacity':0
});
$$('ul#option li.selected').removeProperties('style');
$$('ul#option li.selected').set('html',x);
}).delay(500,this));//$clear(this.retrieve('timerB'));
}
});
});
Mootools Events
works just on first click
, after stops working.
Hope someone have issue for that: http://jsfiddle.net/3j3Ws/
CSS
ul li,li.selected div{
width:22px;
height:22px;
display:block;
background:#000;
color:#fff;
text-align:center;
border-radius:3px;
}
ul#list{
display:none;
opacity:0;
float:left;
}
HTML
<ul id="option">
<li class="selected" id="a">a</li>
<ul id="list">
<li id="b">b</li>
<li id="c">c</li>
<li id="d">d</li>
</ul>
</ul>
Mootools JavaScript
window.addEvent('domready', function(){
var x = '<div>v</div>';
$('ul#option li.selected').set('html',x);
var opt = $('ul#option li.selected div');
var d = opt.getStyle('display');
var l = document.id('list');
var list = opt.set('morph').addEvents({
click:function(){
l.store('timerA',(function(){
l.morph({
'display':'block',
'opacity':1
});
$('ul#option li.selected').setStyle('background-color','#fff');
$('ul#option li.selected div').destroy();
}).delay(10,this));//$clear(this.retrieve('timerA'));
}
}
);
l.set('morph').addEvents({
mouseleave:function(el){
this.store('timerB',(function(){
this.morph({
'display':d,
'opacity':0
});
$('ul#option li.selected').removeProperties('style');
$('ul#option li.selected').set('html',x);
}).delay(500,this));//$clear(this.retrieve('timerB'));
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的写作风格很奇怪。
反正。这是毁灭。事件不被委托。即您的选择器是第一个 div,但它是一个物理元素,它获取 UID 和针对它的功能回调。
通过执行
.destroy()
您将从 dom 中删除此 div,即使您之后重新插入它,因为您不使用事件委托,该事件将不再起作用(事件是元素的一部分存储,因此销毁也会删除它们)。查看 http://jsfiddle.net/dimitar/3j3Ws/1/ ->证明它可以正常工作(我添加了更多的mootools以方便
.show()
和.hide()
但你可以只使用.setStyle("display",或者
,看看为
document.id("option")
执行一个事件click:relay(div.down)
和 mod x html 具有class='down'
- 那么您现在拥有的代码将保留。odd writing style you have.
anyway. it is the destroy. the events are not delegated. i.e. your selector is the first div but that's a physical element that gets a
UID
and a functional cllback against that.by doing
.destroy()
you are removing this div from the dom and even if you reinsert it after, because you don't use event delegation, the event will no longer work (events are part of element storage so destroy removes them too).check out http://jsfiddle.net/dimitar/3j3Ws/1/ -> proves it can work fine (i added mootools more for easy
.show()
and.hide()
but you can just use.setStyle("display", "none")
.alternatively, look at doing an event for
document.id("option")
asclick:relay(div.down)
and mod the x html to haveclass='down'
- then the code you have at the moment will keep.最有可能是这样的:
此时,您正在删除之前插入并附加了点击事件的
。
在稍后的 mouseleave 中,您执行以下操作:
重新创建 div,但尚未将单击处理程序重新附加到新副本。
注释后续:
当您使用
.set('html', x)
时,您将用新节点替换原始节点,这也会替换事件处理程序。处理程序附加到实际节点,而不是 DOM 树中节点的位置。It's most likely this:
At that point, you're deleting the
<div>v</div>
that you inserted earlier and had attached the click event to.In the mouseleave later, you do:
which recreates the div, but has not also reattached the click handler to the new copy.
comment followup:
when you use the
.set('html', x)
, you're replacing the original node with a new one, which also replaces the event handlers. Handlers are attached to an actual node, not to the node's location in the DOM tree.