jQuery '.each' and attaching '.click' event

后端 未结 2 1328
清酒与你
清酒与你 2020-12-12 23:35

I am not a programer but I enjoy building prototypes. All of my experience comes from actionScript2.

Here is my question. To simplify my code I would like to figure

2条回答
  •  星月不相逢
    2020-12-13 00:18

    One solution you could use is to assign a more generalized class to any div you want the click event handler bound to.

    For example:

    HTML:

    
    
    dog
    cat
    mouse

    JS:

    $( ".selected" ).each(function(index) {
        $(this).on("click", function(){
            // For the boolean value
            var boolKey = $(this).data('selected');
            // For the mammal value
            var mammalKey = $(this).attr('id'); 
        });
    });
    

提交回复
热议问题