如果这个话题过头了,请原谅我。我已经阅读了很多帖子,但还没有完全找到我想要的东西,这就是我要问的原因。

我想要一个显示“全部显示”的复选框,当您选中它时会显示多个div。取消选中时,div隐藏

我可以使用复选框显示/隐藏一个具有ID的div,但是我不确定如何使用class元素,因此不必写出每个div ID。这是我正在使用的代码:

<input type="checkbox" id="checkbox">Show All
<div id="links"><a href="#">Link Title</a></div>




$(document).ready(function () {

    $('#link').hide();


    $('#checkbox').change(function () {
        if (!this.checked)
        //  ^
           $('#links').fadeOut('slow');
        else
            $('#links').fadeIn('slow');
    });
});


如何使用JavaScript处理此问题,以便可以在一个复选框中单击显示多个div?也许是这样的课:

<input type="checkbox" id="checkbox">Show All
<div class="links"><a href="#">Link Title</a></div>
<div class="links"><a href="#">Link Title</a></div>
<div class="links"><a href="#">Link Title</a></div>


谢谢!

最佳答案

.用于class,将#用于id

$('.link').hide();
$('#checkbox').change(function () {
    if (!this.checked)
    //  ^
       $('.links').fadeOut('slow');
    else
        $('.links').fadeIn('slow');
});

07-24 09:44
查看更多