从 alt 属性中提取标题并插入到 div 中

发布于 2024-08-08 15:53:02 字数 3081 浏览 2 评论 0原文

我目前在网站上使用相当可爱的 jQuery Slideviewer 1.1 插件,但想从显示的图像中提取 alt 属性,并在适当的时候将它们插入到 div 中。

该插件当前的代码如下所示,供参考:

jQuery(function(){
   jQuery("div.svw").prepend("<img src='/template/theme/designdistillery/img/bg-portfolio-loading.gif' class='ldrgif' alt='loading...'/ >"); 
});
var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
    settings = jQuery.extend({
    easeFunc: "easeInOutExpo",
    easeTime: 750,
    toolTip: false
    }, settings);
    return this.each(function(){
        var container = jQuery(this);
        container.find("img.ldrgif").remove(); // removes the preloader gif
        container.removeClass("svw").addClass("stripViewer");       
        var pictWidth = container.find("img").width();
        var pictHeight = container.find("img").height();
        var pictEls = container.find("li").size();
        var stripViewerWidth = pictWidth*pictEls;
        container.find("ul").css("width" , stripViewerWidth); //assegnamo la larghezza alla lista UL    
        container.css("width" , pictWidth);
        container.css("height" , pictHeight);
        container.each(function(i) {

            jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
            jQuery(this).find("li").each(function(n) {
                        jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");                                             
                });
            jQuery("div#stripTransmitter" + j + " a").each(function(z) {
                jQuery(this).bind("click", function(){

                jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
                var cnt = -(pictWidth*z);
                container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
                return false;
                   });
                }); 


                // next image via image click   14/01/2009
                jQuery("div#stripTransmitter" + j + " a").parent().parent().parent().prev().find("img").each(function(z) {
                jQuery(this).bind("click", function(){
                    var ui  =   jQuery(this).parent().parent().parent().next().find("a");
                    if(z+1 < pictEls){
                        ui.eq(z+1).trigger("click");
                    }
                    else ui.eq(0).trigger("click");
                   });
                });

            jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
            jQuery("div#stripTransmitter" + j + " a:first").addClass("current");
            if(settings.toolTip){
            container.next(".stripTransmitter ul").find("a").Tooltip({
                track: true,
                delay: 0,
                showURL: false,
                showBody: false
                });
            }
            });
        j++;
  });   
};

I am using the rather lovely jQuery slideviewer 1.1 plugin on a site at the moment, but would like to extract the alt attribute from images displayed and insert them into a div at the appropriate time.

The current code for the plugin is shown below for reference:

jQuery(function(){
   jQuery("div.svw").prepend("<img src='/template/theme/designdistillery/img/bg-portfolio-loading.gif' class='ldrgif' alt='loading...'/ >"); 
});
var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
    settings = jQuery.extend({
    easeFunc: "easeInOutExpo",
    easeTime: 750,
    toolTip: false
    }, settings);
    return this.each(function(){
        var container = jQuery(this);
        container.find("img.ldrgif").remove(); // removes the preloader gif
        container.removeClass("svw").addClass("stripViewer");       
        var pictWidth = container.find("img").width();
        var pictHeight = container.find("img").height();
        var pictEls = container.find("li").size();
        var stripViewerWidth = pictWidth*pictEls;
        container.find("ul").css("width" , stripViewerWidth); //assegnamo la larghezza alla lista UL    
        container.css("width" , pictWidth);
        container.css("height" , pictHeight);
        container.each(function(i) {

            jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
            jQuery(this).find("li").each(function(n) {
                        jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");                                             
                });
            jQuery("div#stripTransmitter" + j + " a").each(function(z) {
                jQuery(this).bind("click", function(){

                jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
                var cnt = -(pictWidth*z);
                container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
                return false;
                   });
                }); 


                // next image via image click   14/01/2009
                jQuery("div#stripTransmitter" + j + " a").parent().parent().parent().prev().find("img").each(function(z) {
                jQuery(this).bind("click", function(){
                    var ui  =   jQuery(this).parent().parent().parent().next().find("a");
                    if(z+1 < pictEls){
                        ui.eq(z+1).trigger("click");
                    }
                    else ui.eq(0).trigger("click");
                   });
                });

            jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
            jQuery("div#stripTransmitter" + j + " a:first").addClass("current");
            if(settings.toolTip){
            container.next(".stripTransmitter ul").find("a").Tooltip({
                track: true,
                delay: 0,
                showURL: false,
                showBody: false
                });
            }
            });
        j++;
  });   
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天冷不及心凉 2024-08-15 15:53:02

第一个图像没有标题,因为标题仅在 onclick 之后显示。
这可以通过插入来修复:

var altText = $(this).find("img").eq(0).attr("alt"); 
$("#caption").html(altText).fadeIn("slow");   

The first image has no caption because the caption displays only after onclick.
This can be fixed by inserting:

var altText = $(this).find("img").eq(0).attr("alt"); 
$("#caption").html(altText).fadeIn("slow");   
野鹿林 2024-08-15 15:53:02

它就像这样简单:

$("div").html( $("#myImg").attr("alt") );

您希望什么时候发生这种情况?

It's as simple as:

$("div").html( $("#myImg").attr("alt") );

When do you want this to happen?

就是爱搞怪 2024-08-15 15:53:02

图像通过此行在屏幕上激活(或进入视图),

container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);

您需要将此行修改为:

container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc, function(){
   // z is the index of the currently showing picture, 
   // it comes from the .each call above

   var altText = $(this).find("img").eq(z).attr("alt"); //get alt value

   // now insert the text from the alt value
   $("#id_of_your_text_display_div").html(altText);
});

希望这有帮助......

The image is activated (or brought into view) on screen by this line

container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);

you need to modify this line to:

container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc, function(){
   // z is the index of the currently showing picture, 
   // it comes from the .each call above

   var altText = $(this).find("img").eq(z).attr("alt"); //get alt value

   // now insert the text from the alt value
   $("#id_of_your_text_display_div").html(altText);
});

Hope this helps....

为人所爱 2024-08-15 15:53:02

感谢大家的帮助!

我调整了 ekhaled 的脚本版本,使标题显示在该系列中第一张图像的下方,并为任何后续图像的标题提供了简单的淡入淡出效果 - 现在一切都运行得非常出色!

再次感谢 :)

jQuery(function(){
jQuery("div.svw").prepend("<img src='/template/theme/designdistillery/img/bg-portfolio-loading.gif' class='ldrgif' alt='loading...'/ >"); 
});
var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
        settings = jQuery.extend({
        easeFunc: "easeInOutExpo",
        easeTime: 750,
        toolTip: false
        }, settings);
        return this.each(function(){
            var container = jQuery(this);
            container.find("img.ldrgif").remove(); // Removes the preloader gif
            container.removeClass("svw").addClass("stripViewer");       
            var pictWidth = container.find("img").width();
            var pictHeight = container.find("img").height();
            var pictEls = container.find("li").size();
            var stripViewerWidth = pictWidth*pictEls;
            container.find("ul").css("width" , stripViewerWidth);   
            container.css("width" , pictWidth);
            container.css("height" , pictHeight);
            container.each(function(i) {
                var altTextstart = $(this).find("img").attr("alt");
                $("#caption").html(altTextstart);   
                jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
                jQuery(this).find("li").each(function(n) {
                    jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");                                     
                });
                jQuery("div#stripTransmitter" + j + " a").each(function(z) {
                    jQuery(this).bind("click", function(){  
                        $("#caption").html(altTextstart).hide();    
                        jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current");
                        var cnt = -(pictWidth*z);
                        container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc,function(){
                            // z is the index of the currently showing picture, 
                            // It comes from the .each call above
                            var altText = $(this).find("img").eq(z).attr("alt"); // Get alt value
                            // Now insert the text from the alt value
                            $("#caption").html(altText).fadeIn("slow");
                        });
                        return false;
                    });
                }); 
                jQuery("div#stripTransmitter" + j + " a").parent().parent().parent().prev().find("img").each(function(z) {
                    jQuery(this).bind("click", function(){
                        var ui  =   jQuery(this).parent().parent().parent().next().find("a");
                        if(z+1 < pictEls){
                            ui.eq(z+1).trigger("click");
                            }
                            else ui.eq(0).trigger("click");
                         });
                    });
            jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
            jQuery("div#stripTransmitter" + j + " a:first").addClass("current");                        
            if(settings.toolTip){
                container.next(".stripTransmitter ul").find("a").Tooltip({
                    track: true,
                    delay: 0,
                    showURL: false,
                    showBody: false,
                    });
                }
            });
        j++;
    });
};

Thanks for all the help guys!

I tweaked ekhaled's version of the script so that the caption was displayed underneath the first image in the series, as well as a simple fade effect for captions for any subsequent images - it's all working brilliantly now!

Thanks again :)

jQuery(function(){
jQuery("div.svw").prepend("<img src='/template/theme/designdistillery/img/bg-portfolio-loading.gif' class='ldrgif' alt='loading...'/ >"); 
});
var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
        settings = jQuery.extend({
        easeFunc: "easeInOutExpo",
        easeTime: 750,
        toolTip: false
        }, settings);
        return this.each(function(){
            var container = jQuery(this);
            container.find("img.ldrgif").remove(); // Removes the preloader gif
            container.removeClass("svw").addClass("stripViewer");       
            var pictWidth = container.find("img").width();
            var pictHeight = container.find("img").height();
            var pictEls = container.find("li").size();
            var stripViewerWidth = pictWidth*pictEls;
            container.find("ul").css("width" , stripViewerWidth);   
            container.css("width" , pictWidth);
            container.css("height" , pictHeight);
            container.each(function(i) {
                var altTextstart = $(this).find("img").attr("alt");
                $("#caption").html(altTextstart);   
                jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
                jQuery(this).find("li").each(function(n) {
                    jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");                                     
                });
                jQuery("div#stripTransmitter" + j + " a").each(function(z) {
                    jQuery(this).bind("click", function(){  
                        $("#caption").html(altTextstart).hide();    
                        jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current");
                        var cnt = -(pictWidth*z);
                        container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc,function(){
                            // z is the index of the currently showing picture, 
                            // It comes from the .each call above
                            var altText = $(this).find("img").eq(z).attr("alt"); // Get alt value
                            // Now insert the text from the alt value
                            $("#caption").html(altText).fadeIn("slow");
                        });
                        return false;
                    });
                }); 
                jQuery("div#stripTransmitter" + j + " a").parent().parent().parent().prev().find("img").each(function(z) {
                    jQuery(this).bind("click", function(){
                        var ui  =   jQuery(this).parent().parent().parent().next().find("a");
                        if(z+1 < pictEls){
                            ui.eq(z+1).trigger("click");
                            }
                            else ui.eq(0).trigger("click");
                         });
                    });
            jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
            jQuery("div#stripTransmitter" + j + " a:first").addClass("current");                        
            if(settings.toolTip){
                container.next(".stripTransmitter ul").find("a").Tooltip({
                    track: true,
                    delay: 0,
                    showURL: false,
                    showBody: false,
                    });
                }
            });
        j++;
    });
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文