在每个页面上加载我正在更改图像。我发现这个插件做得很好。
(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
函数,我需要识别图像名称,并且我将重定向到单独的链接。在我的情况下,我不知道如何识别被点击的图像名称。请建议我如何识别被点击的图像名称
希望我的问题清楚。
在此先感谢!!!
答案 0 :(得分: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);
});
答案 1 :(得分: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);
});