Galleria Slideshow api - 查找当前图像的替代文本

发布于 2024-10-20 04:38:33 字数 1152 浏览 3 评论 0原文

我正在一个网站上实现 Galleria 幻灯片,并且大部分情况下它运行良好。 在 div 中(我现在只是登录到 firebug 控制台)

这是我编写的脚本:

<script type="text/javascript">
    Galleria.loadTheme('galleria/themes/classic/galleria.classic.min.js');
    $("#gallery").galleria({
        width: 420,
        height: 370,
        autoplay: 5000, // will move forward every 5 seconds
        extend: function(options) {
            var gallery = this; // "this" is the gallery instance

            this.bind("loadstart", function(e) {
                var currImg = gallery.getActiveImage();
                var altText = $(currImg).attr('alt');
                console.log(altText);
            });
        }
    });
</script>

我正在使用 api 来自定义幻灯片的外观,我想从图像中获取替代文本并显示 这会起作用,但我在控制台中得到“未定义”。有谁知道如何从“Galleria”图像中获取替代文本? API 文档说 .getActiveImage() 方法返回一个返回 IMG 元素,因此我不确定我是否以正确的方式使用 IMG 元素,或者它是否具有可供我抓取的替代文本。

我还尝试使用我的图像 ID,但这

('slideshow_image_' + e.index)

也在日志中给了我一个未定义的结果。

我知道 HTML 中有一个名为 Slideshow_image_0 等的元素,我认为通过元素 ID 找到替代文本并显示它会很容易。如果我查看 firebug 的 HTML 选项卡,整个 div 完全不同,并且图像由 galleria 动态填充到各个部分中。不知道该怎么办...请帮忙!

I'm implementing the Galleria slideshow on a site and I've got it working fine for the most part. I'm working with the api to customize the appearance of my slideshow and I want to get the alt text from the image and display in a div (I'm just logging to the firebug console for now)

Here's the script I wrote:

<script type="text/javascript">
    Galleria.loadTheme('galleria/themes/classic/galleria.classic.min.js');
    $("#gallery").galleria({
        width: 420,
        height: 370,
        autoplay: 5000, // will move forward every 5 seconds
        extend: function(options) {
            var gallery = this; // "this" is the gallery instance

            this.bind("loadstart", function(e) {
                var currImg = gallery.getActiveImage();
                var altText = $(currImg).attr('alt');
                console.log(altText);
            });
        }
    });
</script>

Seems that this would work, but I'm getting "undefined" in the console. Does anyone know how to get the alt text from a "Galleria" image? The API documentation says that the .getActiveImage() method returns an returns IMG Element, so I'm not sure if I'm working with the IMG element in the proper way or if it even has the alt text available for me to grab.

I also tried using my image ID, which is

('slideshow_image_' + e.index)

but that also gave me an undefined result in the log.

I know there is an element in the HTML that is called slideshow_image_0, etc, and I would think that it would be easy to find the alt text by element ID and display it. If I look at firebug's HTML tab, the whole div is completely different and the images are dynamically populated into the sections by galleria. Not sure what to do... please help!

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

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

发布评论

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

评论(1

欲拥i 2024-10-27 04:38:33

您需要绑定一个在图像更改时调用的方法。

而且 getActiveImage() 并不像 getData 那样有用。下面的代码在控制台中打印您想要的 ALT 和标题,以防万一您需要。

试试这个:

$("#gallery").galleria({
        width: 420,
        height: 370,
        autoplay: 5000, // will move forward every 5 seconds
        extend: function(options) {
            var gallery = this; // "this" is the gallery instance

            this.bind(Galleria.IMAGE, function(e) {
                var current = gallery.getData(gallery.getIndex());
                var currImg = current.original;
                var altText = $(currImg).attr('alt');
                console.log(altText, current.title);
            });
        }
    });

You need to bind a method that's called when image change.

Also getActiveImage() is not quite useful, as getData is. The following piece of code, prints in console the ALT you wanted, and the title, just in case you want.

Try this:

$("#gallery").galleria({
        width: 420,
        height: 370,
        autoplay: 5000, // will move forward every 5 seconds
        extend: function(options) {
            var gallery = this; // "this" is the gallery instance

            this.bind(Galleria.IMAGE, function(e) {
                var current = gallery.getData(gallery.getIndex());
                var currImg = current.original;
                var altText = $(currImg).attr('alt');
                console.log(altText, current.title);
            });
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文