获取图像src名称javascript

时间:2011-09-22 22:58:03

标签: javascript regex

我想

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>

3 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

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

答案 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];
  });
});

答案 2 :(得分:0)

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

});