简体   繁体   English

使用Fancybox仅获取第一个数据库条目

[英]Getting only first database entry with Fancybox

I have a while loop that gets the images from my database. 我有一个while循环,可以从数据库中获取图像。 if you click on the image, a box pops up with the corresponding name. 如果单击图像,则会弹出一个带有相应名称的框。

So if the image of an ape is connected to the text "banana" in the database, the popup that occurs when you click on the image of the ape contains the word "banana". 因此,如果将猿猴的图像连接到数据库中的文本“香蕉”,则单击猿猴的图像时出现的弹出窗口将包含单词“ banana”。

I want to achieve this with Fancybox2. 我想用Fancybox2实现。 Everything works fine, but the text in the popup only shows the first entry of the database. 一切正常,但弹出窗口中的文本仅显示数据库的第一个条目。 I've four images in my database and they are all on the screen, but no matter on which image you click, the text in the popup is the text of the first database entry. 我的数据库中有四个图像,它们都在屏幕上,但是无论您单击哪个图像,弹出窗口中的文本都是第一个数据库条目的文本。

I've done my research on the internet and found multiple solutions, like "use classes instead of id's in the jquery file", but none of them are working. 我已经在互联网上进行了研究,发现了多种解决方案,例如“在jquery文件中使用类而不是id”,但是它们都不起作用。

Here's my code: 这是我的代码:

HTML 的HTML

<a class="fancybox" href="#data">
    <img src="<?php echo $row['image'];?>"/>
</a>


<div id="data">
    <p><?php echo $row['name'];?></p>
</div>

jQuery jQuery的

$(document).ready(function () {
    $(".fancybox").fancybox();
});

I know that I use an ID to refer to the content, but I don't know how to make it a class. 我知道我使用ID来引用内容,但是我不知道如何使其成为类。 I hope someone can help me out. 我希望有人能帮助我。

If this is an duplicate question, my apologies. 如果这是一个重复的问题,我表示歉意。 I'm new here and I didn't find any other solutions that could help me 我是新来的,没有找到其他可以帮助我的解决方案

Thanks in advance! 提前致谢!

I haven't used fancybox in a while, but I am pretty sure that all you need to do, is set the title attribute: 我已经有一段时间没有使用fancybox了,但是我很确定您要做的就是设置title属性:

<a class="fancybox" href="#data" title="<?php echo $row['name'];?>">
    <img src="<?php echo $row['image'];?>"/>
</a>

Agree with @Jeroen: you may not need the #data division, neither displaying the content as inline but as html (directly from the link) so try adding a (HTML5) data-* attribute to your <a> tags like : 同意@Jeroen:您可能不需要#data分区,也没有将内容显示为inline而是显示为html (直接从链接显示),因此请尝试在您的<a>标签中添加(HTML5) data-*属性,例如:

<a class="fancybox" href="#" data-content="<?php echo $row['name'];?>">
    <img src="<?php echo $row['image'];?>" alt=""/>
</a>

Then, get the content from the data attribute and set type to html , using the beforeLoad callback like : 然后,从data属性获取内容并将type设置为html ,使用beforeLoad回调类似:

jQuery(document).ready(function ($) {
    $(".fancybox").fancybox({
        type: "html",
        beforeLoad: function () {
            this.content = $(this.element).data("content");
        }
    });
});

See JSFIDDLE 参见JSFIDDLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM