Binding click to a repeated div ID

后端 未结 7 2024
孤独总比滥情好
孤独总比滥情好 2021-01-23 10:33

I\'m trying to create a div popup gallery type thing. But I\'m having an issue with my approach.

I have a few linked thumbnail images, which link to the lar

相关标签:
7条回答
  • 2021-01-23 11:26

    Use class instead of ID, repeating ID's isn't a good idea.

    Using classes you solve your problem: jsfiddle

    <a href="www.google.com" class="galImage">
    <img src="http://www.savoryspiceshop.com/content/mercury_modules/cart/items/3/0/3/3030/rogan-josh-salt-free-thumbnail-fw232.jpg">
    </a>
        <br />
    <a href="www.yahoo.com" class="galImage">
    <img src="http://www.savoryspiceshop.com/content/mercury_modules/cart/items/2/9/8/2986/italian-herbs-salt-free-thumbnail-fw232.jpg">
    </a>
    
    $('.galImage').click(function(e){
            e.preventDefault();
            alert($(this).attr("href"));
        });
    
    0 讨论(0)
提交回复
热议问题