duandanbeng1829 2015-11-16 08:26
浏览 9
已采纳

too long

I'm trying to make a page that gets entries from a database and displays them in a <div> called "gallery-item". However the lightbox works, it only shows the first entry of the database. I hope someone can make sense of this code and may be able to help me with my problem.

My html/php/sql

<?php if (isset($_POST["Website"])) {
            $query=$db->prepare("SELECT * FROM `portfoliodb`.`website`");
            $query->execute();

            while   ( $row = $query->fetch()){
                $website_id = $row['website_id'];
                $website_name = $row ['website_name'];
                $website_desc_en = $row ['website_desc_en'];
                $website_tags = $row ['website_tags'];
                $website_image = $row ['website_image'];
                $website_type = $row['website_type'];
                echo'
                    <div class="background hidden" id="background"></div>
                        <a>
                            <div class="lightbox hidden" id="lightbox">
                                <div class="box-title hidden" id="box-title">
                                    <h4 class="hidden" id="box-title-h">' . htmlspecialchars($website_name) . '</h4>
                                    <h4 class="close hidden" id="close">X</h4>
                                </div>
                                <div class="box-img hidden" id="box-img">

                                </div>
                                <div class="box-foot hidden" id="box-foot">
                                    <div class="box-space hidden" id="box-space"></div>
                                    <div class="box-desc hidden" id="box-desc">
                                        <p class="desc-text hidden" id="box-text">'. htmlspecialchars($website_desc_en) .' </p>
                                    </div>
                                    <div class="tag-space-box hidden" id="box-tags-space"></div>
                                    <div class="tag-box hidden" id="box-tags">
                                        <small id="box-small" class="hidden">'. htmlspecialchars($website_tags) .'</small>
                                    </div>
                                </div>
                            </div>
                        </a>
                    <a>
                    <div class="gallery-item-web button" id="button">
                        <p class="gallary-item-text">';
                echo htmlspecialchars($website_name);
                echo'</p>';
                echo '<i class="fa fa-desktop fa-3x position-icon"></i>
                        </div></a>
                        ';
            }}

The gallery shows all entries in the database so that works fine, the only problem is that the lightbox shows the first entry, so if I were to have a database with the entries "Apples, Pears, Oranges", the gallery would display "Apples, Pears, Oranges". But the lightbox would display "Apples" on every single entry.

My Jquery

$(document).ready(function(){
$(".button").click(function(){
    $("#background").toggleClass("hidden");
    $("#lightbox").toggleClass("hidden");
    $("#box-title").toggleClass("hidden");
    $("#box-title-h").toggleClass("hidden");
    $(".close").toggleClass("hidden");
    $("#box-img").toggleClass("hidden");
    $("#box-foot").toggleClass("hidden");
    $("#box-space").toggleClass("hidden");
    $("#box-desc").toggleClass("hidden");
    $("#box-text").toggleClass("hidden");
    $("#box-tags-space").toggleClass("hidden");
    $("#box-tags").toggleClass("hidden");
    $("#box-small").toggleClass("hidden");
});
$(".close").click(function(){
    $("#background").toggleClass("hidden");
    $("#lightbox").toggleClass("hidden");
    $("#box-title").toggleClass("hidden");
    $("#box-title-h").toggleClass("hidden");
    $(".close").toggleClass("hidden");
    $("#box-img").toggleClass("hidden");
    $("#box-foot").toggleClass("hidden");
    $("#box-space").toggleClass("hidden");
    $("#box-desc").toggleClass("hidden");
    $("#box-text").toggleClass("hidden");
    $("#box-tags-space").toggleClass("hidden");
    $("#box-tags").toggleClass("hidden");
    $("#box-small").toggleClass("hidden");
});
$(".background").click(function(){
    $("#background").toggleClass("hidden");
    $("#lightbox").toggleClass("hidden");
    $("#box-title").toggleClass("hidden");
    $("#box-title-h").toggleClass("hidden");
    $(".close").toggleClass("hidden");
    $("#box-img").toggleClass("hidden");
    $("#box-foot").toggleClass("hidden");
    $("#box-space").toggleClass("hidden");
    $("#box-desc").toggleClass("hidden");
    $("#box-text").toggleClass("hidden");
    $("#box-tags-space").toggleClass("hidden");
    $("#box-tags").toggleClass("hidden");
    $("#box-small").toggleClass("hidden");
});

});

(It's pretty bad I know, this is my first time using Jquery)

This is the Jquery for the lightbox, it toggles the class on every item of the lightbox to "hidden". Whih basically makes every item with that class invisible, great for a lightbox.

  • 写回答

2条回答 默认 最新

  • dongyong6045 2015-11-16 08:52
    关注

    First off, you don't need to add class of "hidden" to every element. You can just add it to the container element and then all tags inside would also be hidden.

    Your main mistake was to have the same ids and classes for all images, you will need to ensure that each image has an unique identifier.

    Try my solution: https://jsfiddle.net/3vhv90ce/2/

    HTML:

    <div class="container1">
        <div class="background" id="background"></div>
            <a>
                <div class="lightbox" id="lightbox">
                    <div class="box-title" id="box-title">
                        <h4 class="" id="box-title-h">name</h4>
                        <h4 class="close" id="close">X</h4>
                    </div>
                    <div class="box-img" id="box-img">
    
                    </div>
                    <div class="box-foot" id="box-foot">
                        <div class="box-space" id="box-space"></div>
                        <div class="box-desc" id="box-desc">
                            <p class="desc-text" id="box-text">description</p>
                        </div>
                        <div class="tag-space-box" id="box-tags-space"></div>
                        <div class="tag-box" id="box-tags">
                            <small id="box-small" class="">tags</small>
                        </div>
                    </div>
                </div>
            </a>
        </div>
    </div>
    <div class="gallery-item-web button" data-id="1">
        <p class="gallary-item-text">Click me</p>
        <i class="fa fa-desktop fa-3x position-icon"></i>
    </div>
    
    <div class="container2">
        <div class="background" id="background"></div>
            <a>
                <div class="lightbox" id="lightbox">
                    <div class="box-title" id="box-title">
                        <h4 class="" id="box-title-h">name</h4>
                        <h4 class="close" id="close">X</h4>
                    </div>
                    <div class="box-img" id="box-img">
    
                    </div>
                    <div class="box-foot" id="box-foot">
                        <div class="box-space" id="box-space"></div>
                        <div class="box-desc" id="box-desc">
                            <p class="desc-text" id="box-text">description</p>
                        </div>
                        <div class="tag-space-box" id="box-tags-space"></div>
                        <div class="tag-box" id="box-tags">
                            <small id="box-small" class="">tags</small>
                        </div>
                    </div>
                </div>
            </a>
        </div>
    </div>
    <div class="gallery-item-web button" data-id="2">
        <p class="gallary-item-text">Click me</p>
        <i class="fa fa-desktop fa-3x position-icon"></i>
    </div>
    

    JS:

    $(document).ready(function(){
        $(".button").click(function(){
            $('.container'+$(this).data('id')).toggleClass("hidden");
        });
    });
    

    CSS:

    .hidden {
        display: none;
    }
    .gallery-item-web {
        cursor: pointer;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向