关于JS获取多个div下多个img的src的问题

2024-11-26 02:47:11
推荐回答(3个)
回答1:

由于html代码中,id是不允许重复的,所以,我把id,修改成了class


            
                
                    
                .........
                
            

            
                
                    
                .........
                
            

            
                
                    
                .........
                
            

        
    
    
        $(function () {
            var arrayImgs = [];
            $("div.info img.marginImg").each(function () {
                alert($(this).attr("src"));
                arrayImgs.push($(this).attr("src"));
            });
        });
    

回答2:

id不允许重复,给img增加name属性,所有img的name都相同,然后用document.getElementsByName取所有img,代码如下


               
                    
                .........
                
            

            
                
                    
                .........
                
            

            
                
                    
                .........
                

 var imgs = document.getElementsByName("marginImg");

回答3:

百度搜搜可以搜到

相关问答
最新问答