2

在每个页面加载时,我都在更改图像。我发现这个插件做得很好。

(function($){

    $.randomImage = {
        defaults: {

            //you can change these defaults to your own preferences.
            path: '/templates/default/images/', //change this to the path of your images
            myImages: ['fab_ad1.jpg', 'fab_ad2.jpg', 'fab_ad3.jpg'] //put image names in this bracket. ex: 'harold.jpg', 'maude.jpg', 'etc'

        }           
    }

    $.fn.extend({
            randomImage:function(config) {

                var config = $.extend({}, $.randomImage.defaults, config); 

                 return this.each(function() {

                        var imageNames = config.myImages;

                        //get size of array, randomize a number from this
                        // use this number as the array index

                        var imageNamesSize = imageNames.length;

                        var lotteryNumber = Math.floor(Math.random()*imageNamesSize);

                        var winnerImage = imageNames[lotteryNumber];

                        var fullPath = config.path + winnerImage;


                        //put this image into DOM at class of randomImage
                        // alt tag will be image filename.
                        $(this).attr( {
                                        src: fullPath,
                                        alt: winnerImage
                                    });


                }); 
            }

    });



})(jQuery);

HTML:

<img class="shuffle" src="" alt="" onclick="chooseLink()">

调用这个函数:

$('.shuffle').randomImage();

使用此功能,我在我的页面中显示广告。我对每个广告图片都有单独的链接。当单击图像时,我正在调用一个javascript函数,并且我需要识别图像名称,并且我将重定向到单独的链接。就我而言,我不知道如何识别被点击的图像名称。请建议我如何识别被点击的图像名称。
希望我的问题很清楚。提前致谢 !!!

4

2 回答 2

2

改变

<img class="shuffle" src="" alt="" onclick="chooseLink()">

<img class="shuffle" src="">

并将其添加到您的脚本中:

$('.shuffle').click(function(){
    var imageId = this.src.split('/').pop(); // this is the name of your image file
                                             // It identifies the image
    // use the image 
    chooseLink(imageId);
});
于 2013-01-17T10:04:17.107 回答
0

你可以稍微修改你的图像html:

<img class="shuffle" src="" alt="">

然后添加这个

$('.shuffle').randomImage().click(function(){
    var imgsrc = $('.shuffle').attr('src');
    var imgname = imgsrc.substr(imgsrc.lastIndexOf('/')+1);
    chooseLink(imgname);
});
于 2013-01-17T10:15:15.920 回答