2011-09-22 86 views
0

我想有獲取圖片src名稱的JavaScript

album_name = Precious 

如何獲得呢?

<html> 
    <head> 
     <title></title> 
    </head> 
    <body> 
     <div id="ps_slider" class="ps_slider"> 
      <div id="ps_albums"> 
       <div class="ps_album"> 
        <div class="ps_image"> 
         <div class="ps_img"> 
          <img src="puppies/Snoopy/primary.jpg" alt="Dachshund Puppy Thumbnail"/> 
         </div> 
        </div> 
       </div> 
       <div class="ps_album"> 
        <div class="ps_image"> 
         <div class="ps_img"> 
          <img src="puppies/Precious/primary.jpg" alt="Dachshund Puppy Thumbnail"/> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 

     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
     <script type="text/javascript"> 
     $(function() { 
      var $ps_albums  = $('#ps_albums'); 

      $ps_albums.children('div').bind('click',function(){ 
       var $elem = $(this); 
       var album_name = 'album' + parseInt($elem.index() + 1); 
       console.log(album_name); 
      }); 

     }); 
     </script> 

    </body> 
</html> 

回答

0
$(function() { 
    $(".ps_img").click(function(){ 
     var album = $(this).find("img").attr("src").split("/")[1]; 
     alert(album); 
    }); 

}); 
0

這應該做的伎倆:

$ps_albums.children('div').bind('click',function(){ 
    var album_name = $(this).find('img').attr('src').split('/')[1]; 
}); 
0

試試這個:

$(function() { 
    $('#ps_albums .ps_album').click(function() { 
    var $elem = $(this); 
    var $img = $elem.find('img'); 
    var album_name = $img.attr('src').split('/')[1]; 
    }); 
});