实施起来并不难。在一个非常原始(和未测试)的形式中,它是这样的:
(function($){
$.fn.imageTag = function(options){
var o = $.extend({}, $.fn.imageTag.options, options||{});
this.each(function(){
var selections = $.map(o.formats, function(e,i){
return 'a[href$=.'+e+']';
}).join(',');
$(selections, this).each(function(){
var img = $('<img />').attr('src', $(this).attr('href'));
var tag;
if(o.wrapper){
tag = $(o.wrapper).append(img);
} else {
tag = img;
}
if(o.css){
tag.css(o.css);
}
$(this).replaceWith(tag);
});
});
return this;
};
$.fn.imageTag.options = {
'formats': ['png','gif','jpg'],
'wrapper': null, // null or a tag like '<div></div>'
'css': null // null or a hash of css properties to be used as an arg for css()
};
})(jQuery);
当然,它不处理从
a
到
img
或可选包装元素。