问题描述
我正在尝试使用mootools快速完成点击时切换一些文本的任务.但是我似乎找不到与jQuery的toggle()函数相当的Mootools.
I'm using trying to use mootools for a quick task of toggling some text on click. But I can't seem to find a Mootools equivalent of jQuery's toggle() function.
我想做的事情如下:
$('a#id').toggle(
function() {
$(this).set('html', 'new text');
},
function() {
$(this).set('html', 'old text');
}
);
如何修改上述mootools代码?
How would I modify the above code for mootools?
谢谢!
推荐答案
据我所知,mootools上的切换"不存在.
As far as I know that "toggle" on mootools doesn't exists.
例如,可以通过以下方式实现"它: http://www.jsfiddle .net/steweb/rdvx8/
You could "implement" it, by this way for example: http://www.jsfiddle.net/steweb/rdvx8/
这是您想要获得的吗?
编辑(对于mootools开发人员:D),这可能是创建常规切换"的一种方式:
Edit (for mootools developers :D ), this could be a way to create a general "toggle":
Element.implement({
toggle: function(fn1,fn2){
this.store('toggled',false);
return this.addEvent('click',function(event){
event.stop();
if(this.retrieve('toggled')){
fn1.call(this);
}else{
fn2.call(this);
}
this.store('toggled',!(this.retrieve('toggled')));
});
}
});
//using it in a 'jQuery' mode
$('mya').toggle(
function() {
this.set('html', 'new text');
},
function() {
this.set('html', 'old text');
}
);
在这里拨弄: http://www.jsfiddle.net/steweb/qBZ47/
编辑2 签出改进的版本 http://jsfiddle.net/dimitar/UZRx5/(感谢 @Dimitar Christoff )
edit 2 check out an improved version http://jsfiddle.net/dimitar/UZRx5/ (thanks @Dimitar Christoff)
这篇关于Mootools是否等效于jQuery toggle()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!