简体   繁体   中英

How to hide DIVs using a class name?

My code:

var array1 = document.getElementsByClassName("div");
var array2 = document.getElementsByClassName("button");

    for(var i = 0; i < array1.length; i++)
    {
        $(array2[i]).show();
        $(array1[i]).hide();

        $(array2[i]).click(function(){
            $(array1[i]).slideToggle();
        });
    }

Why I got error: Could not convert JavaScript argument arg 0?

var $buttons = $(".button").hide();

$(".div").show().bind("click", function(event) {
    var index = $divs.index(this);

    $buttons.eq(index).slideToggle();
});

OR:

var $buttons = $(".button").hide(),
    $divs = $(".div").show();

$.each($buttons, function(index) {
    var $button = $(this);
    $divs.eq(index).bind("click", function() {
        $button.slideToggle();
    });
});

OR

var $buttons = $(".button").hide(),
    $divs = $(".div").show();

$buttons.each(function(index) {
    var $button = $(this);
    $divs.eq(index).bind("click", function() {
        $button.slideToggle();
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM