# JQuery toggle事件函数的替换方案

**URL:** <https://www.shiqidu.com/t/topic/251>\
**Category:** 开发调优\
**Tags:** jQuery\
**Created:** [October 22, 2017, 3:52pm UTC](https://www.shiqidu.com/t/topic/251 "2017-10-22T15:52:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
**Post date:** [October 22, 2017, 3:52pm UTC](https://www.shiqidu.com/t/topic/251/1 "2017-10-22T15:52:03Z")

</div>

`toggle`是干啥使得？

可以用来切换`dom`的显示和隐藏，也可以切换两个或多个事件。

比如以前我会这么用

```javascript
$(&quot;.see-more-profile-switch&quot;).toggle(function () {  
        console.log('我被显示了。')  
    }, function () {  
        console.log('我被隐藏了。')  
    })  

```

如此简单的代码就实现了事件的轮询。  
是不是很强大？

可惜在jQuety1.9版本的时候，`toggle`函数被废弃掉了。  
那么怎么实现相似的功能呢？其实也很容易。

```auto
var sth = 0;  
    $(document).on('click', '.see-more-profile-switch', function () {  
        if (sth == 0) {  
            console.log('我被显示了。')  
        } else {  
            console.log('我被隐藏了。')  
        }  
        sth = !sth;  
    })  

```
