jQuery toggle() function not working

柔情痞子 提交于 2019-11-29 13:08:38

http://api.jquery.com/category/deprecated/

Note: This method signature was deprecated in jQuery 1.8 and removed in jQuery 1.9. jQuery also provides an animation method named .toggle() that toggles the visibility of elements. Whether the animation or the event method is fired depends on the set of arguments passed.

http://api.jquery.com/toggle-event/

Updated after OP's comment

DEMO

if you want to make this code work in any version of jQuery

$(document).ready(function () {
    var arr = ['yes', 'no'],
        i = 0;
    $('#btn').click(function () {
        $('#msg').html(arr[i++ % 2]);
    });
});

DEMO

if you want to make use of toggle-event like functionality in future

use .one()

$(document).ready(function () {
    function handle1() {
        $('#msg').html('yes');
        $('#btn').one('click', handle2);
    }

    function handle2() {
        $('#msg').html('no');
        $('#btn').one('click', handle1);
    }
    $('#btn').one('click', handle1);
});

or

Toggle was removed in version 1.9, so you cannot use it - if you want this logic can be manually implemented or you can make use of the migration plugin

to include the migration plugin, after the inclusion of jQuery library add

<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!