javascript - jQuery 切换替代方法?

标签 javascript jquery

由于toggle已被弃用,我用它来togle div:

$("#syndicates_showmore").on("click", function () {

    if (!clicked) {
        $('#syndicates').fadeTo('slow', 0.3, function () {
            $(this).css(
            {
                'height': 'auto',
                'overflow': 'none'
            });
        }).fadeTo('slow', 1);

        setTimeout(function () {
            $("#syndicates_showmore").text("Show less");
        }, 500);

        clicked = true;
    }
    else {
        $('#syndicates').fadeTo('slow', 0.3, function () {
            $(this).css(
            {
                'height': '290px',
                'overflow': 'hidden'
            });
        }).fadeTo('slow', 1);

        setTimeout(function () {
            $("#syndicates_showmore").text("Show more");
        }, 500);

        clicked = false;
    }
});

有没有更干净的方法来做到这一点?

最佳答案

根据jQuery 1.9 Upgrade Guide :

.toggle(function, function, ... ) removed

This is the "click an element to run the specified functions" signature of .toggle(). It should not be confused with the "change the visibility of an element" of .toggle() which is not deprecated. The former is being removed to reduce confusion and improve the potential for modularity in the library. The jQuery Migrate plugin can be used to restore the functionality.

换句话说,您仍然可以像这样使用.toggle:

var clicked = false;
$("#syndicates_showmore").on("click", function () {
    clicked = !clicked;
    var showText = "Show " + (clicked ? "more" : "less");
    $('#syndicates').toggle(function () {
        $("#syndicates_showmore").text(showText);
    });
});

关于javascript - jQuery 切换替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19774843/

相关文章:

javascript - 删除 jquery 上的内容

javascript - 像老板一样根据数据在 D3 中设置颜色

javascript - 设置变量并传递给函数还是重新设置变量?

jquery - 更改类函数

javascript - jQuery UI - 未捕获类型错误 : Object [object Object] has no method 'tabs' (in jade)

javascript - Angular Material : layout-fill not filling parent <div>

javascript - 为什么引导模式关闭事件 "stack"?

javascript - jQuery 加载方法字符集

javascript - Intl.NumberFormat#格式化最大整数值

php - 使用下拉菜单和文本输入在 mysql 数据库上执行 AJAX 搜索