Mutiple buttons with the same name showing different divs

后端 未结 4 1737
名媛妹妹
名媛妹妹 2021-01-28 07:42

I have a large amount of buttons all used to display a div before it, my question is instead of having 20 different javascript functions which all do the same thing, is it possi

4条回答
  •  隐瞒了意图╮
    2021-01-28 08:12

    You could write a function like this:

    function yourfunction(id1, id2) {
        document.getElementById(id1).addEventListener("click", function(){
            document.getElementById(id2).style.display="block";
        });
    }
    

    And call it in your script:

    yourfunction("#drop-button", "#dropdown");
    yourfunction("#drop-button1", "#dropdown1");
    // more ids
    

提交回复
热议问题