how to toggle attr() in jquery

前端 未结 10 1940
独厮守ぢ
独厮守ぢ 2021-01-30 15:50

I have a simple add attribute function:

$(\".list-toggle\").click(function() {
    $(\".list-sort\").attr(\'colspan\', 6);
});

My question is:

10条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 16:19

    $('.list-toggle').click(function() {
        var $listSort = $('.list-sort');
        if ($listSort.attr('colspan')) {
            $listSort.removeAttr('colspan');
        } else {
            $listSort.attr('colspan', 6);
        }
    });
    

    Here's a working fiddle example.

    See the answer by @RienNeVaPlus below for a more elegant solution.

提交回复
热议问题