我怎样才能缩短这个函数jQuery?

时间:2013-01-25 18:29:43

标签: jquery image function gallery

如何缩短这一点以使其全部适合一个功能。我有一个工作的图库,当点击类a的图像时,类bigimg中的图像被更改为选中的图像。我可能已经做了很长一段时间来改变图像,但最多只会有十几个。

顺便说一句,我将主持我的图像,这些仅供我测试

HTML / jQuery

<div id="galleryholder">

            <div id="largeimg" class="center">
                <img class="bigimg" src="http://1.bp.blogspot.com/-BNkayp7vphQ/UEmx3WTwe1I/AAAAAAAAELg/eHu4hWtoXbk/s1600/desktop-wallpaper-29.jpg">
            </div>

            <div id="reel">
                <a href=""><img class="a" src="http://wallpaperstate.org/wp-content/gallery/ca_home/hd-wallpaper-30.jpg"></a>
                <a href="#"><img class="b" src="http://1.bp.blogspot.com/-BNkayp7vphQ/UEmx3WTwe1I/AAAAAAAAELg/eHu4hWtoXbk/s1600/desktop-wallpaper-29.jpg"></a>
                <a href="#"><img class="c" src="http://1.bp.blogspot.com/-BNkayp7vphQ/UEmx3WTwe1I/AAAAAAAAELg/eHu4hWtoXbk/s1600/desktop-wallpaper-29.jpg"></a>
            </div>

                <script>
                $(function() {

                  $('.a').click(function(){
                    $(".bigimg").attr('src',"http://wallpaperstate.org/wp-content/gallery/ca_home/hd-wallpaper-30.jpg");
                    return false;
                  });
                });


                $(function() {
                  $('.b').click(function(){
                    $(".bigimg").attr('src',"http://1.bp.blogspot.com/-BNkayp7vphQ/UEmx3WTwe1I/AAAAAAAAELg/eHu4hWtoXbk/s1600/desktop-wallpaper-29.jpg");
                    return false;
                  });
                });
                </script>

        </div>

2 个答案:

答案 0 :(得分:8)

从点击的图片中动态拉出src

var $big = $(".bigimg");

$('#reel img').click(function () {
    $big.attr('src', this.src);
    return false;
});

答案 1 :(得分:0)

        <div id="largeimg" class="center">
            <img class="bigimg" src="http://1.bp.blogspot.com/-BNkayp7vphQ/UEmx3WTwe1I/AAAAAAAAELg/eHu4hWtoXbk/s1600/desktop-wallpaper-29.jpg">
        </div>

        <div id="reel">
            <a href=""><img class="a" src="http://wallpaperstate.org/wp-content/gallery/ca_home/hd-wallpaper-30.jpg"></a>
            <a href="#"><img class="a" src="http://1.bp.blogspot.com/-BNkayp7vphQ/UEmx3WTwe1I/AAAAAAAAELg/eHu4hWtoXbk/s1600/desktop-wallpaper-29.jpg"></a>
            <a href="#"><img class="a" src="http://1.bp.blogspot.com/-BNkayp7vphQ/UEmx3WTwe1I/AAAAAAAAELg/eHu4hWtoXbk/s1600/desktop-wallpaper-29.jpg"></a>
        </div>

            <script>
            $(function() {
              $('.a').click(function(e){
                $(".bigimg").attr('src',$(this).attr('src'));
                e.preventDefault();
              });
            });
            </script>