使用触发器切换 [英] Use trigger for toggle

查看:88
本文介绍了使用触发器切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < span id =test>点击< / span> 

$('#test')。toggle(function(){
alert('true');
},function(){
alert('false');
});


$('#test')。trigger('click');





这工作正常,但是我想在触发器中调用第二个触发器 - 带有 alert('false')。现在,它始终是 alert('true')



我该怎么做?我不想改变功能切换 - 只能触发应该改变。



http://jsfiddle.net/xSbTC/

解决方案

如果我正确理解你的问题,重新尝试这样做,而不必在第二个切换函数中重复代码。为了做到这一点,只需将其移动到一个单独的函数,然后调用它...

  function toggle1(){
alert(true);
}
function toggle2(){
alert(false);

$ b $('#test')。toggle(function(){
toggle1();
},function(){
toggle2 ();
});

toggle2();


为了清晰起见,我已经将切换的两个部分的代码放入单独的函数中。如果你愿意的话,你显然可以将代码从 toggle1()移动到第一个切换函数中。

http://jsfiddle.net/xSbTC/4/


<span id="test">​click</span>​​​​​​​​​​​​​​

$('#test').toggle(function() {
    alert('true');
}, function() {
    alert('false');
});


$('#test').trigger('click');

This is working ok, but I would like call the trigger for second the function in toggle - with alert('false'). Now, it is always alert('true').

How can I make it? I don't want changed function toggle - only trigger should be changed.

http://jsfiddle.net/xSbTC/

解决方案

If I understand your issue correctly, you're trying to do this without having to repeat the code in the 2nd toggle function. In order to do this, just move it to a separate function and call that instead...

function toggle1() {
    alert("true");
}
function toggle2() {
    alert("false");
}

$('#test').toggle(function() {
    toggle1();
}, function() {
    toggle2();
});

toggle2();

​ I've put the code for both parts of toggle into separate functions for clarity. You could obviously move the code from toggle1() into the first toggle function if you wanted to.

http://jsfiddle.net/xSbTC/4/

这篇关于使用触发器切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆