Access Multiple Elements of same ID in jQuery

前端 未结 4 705
离开以前
离开以前 2020-11-29 11:15

If i have elements such as this




         


        
4条回答
  •  猫巷女王i
    2020-11-29 11:47

    Do not create markup that contains elements with duplicate IDs. This will break things, and you will be mauled by a velociraptor faster than you can say "goto".

    Use classes instead:

    
    
    
    

    then...

    $(document).ready(function() {
        $('.myEle').live('mouseup', function () {
    
            $('#images').attr("src", myEle.getNumber() + ".jpg"); 
        });
    });
    

    Re: OP comment

    "How do i know which image is pressed?"

    Use the this keyword:

    $(document).ready(function() {
        $('.myEle').live('mouseup', function () {
    
            $('#images').attr("src", $(this).attr('src')); 
        });
    });
    

    ...I think that's what you're looking for.

    velociraptor

提交回复
热议问题