代码之家  ›  专栏  ›  技术社区  ›  Mark

jquery图像链接转化为图像标记

  •  0
  • Mark  · 技术社区  · 14 年前

    有没有一个jquery插件可以识别图像URL并将其转换为图像链接,就像 http://videojs.com/ 适用于视频或jquery媒体作品 http://jquery.malsup.com/media/#overview

    显然这些插件都不支持图像????

    1 回复  |  直到 14 年前
        1
  •  2
  •   prodigitalson    14 年前

    实施起来并不难。在一个非常原始(和未测试)的形式中,它是这样的:

    (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 或可选包装元素。