javascript 为什么找不到动态添加的元素?

rekjcdws  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(157)

我用拇指和主图像创建了fancybox,但我有问题,如果我关闭它,下次打开,那么标记的拇指不是实际的一个,而是最后一个。
我想通过比较主图像和拇指图像的源代码,使代码将根据实际图像改变它,但我找不到通过 AJAX 动态添加的拇指图像。
你能给我点建议吗?

// Show fancybox after click on a elemet of gallery
gallery.find('a').on('click', function(event) {
    event.preventDefault();

    // Show fancybox
    overlay.appendTo('body');

    // Define variables
    var galleryImgWrapperHref = $(this).attr('href');

    // Show chosen image in fancybox
    image.attr('src', galleryImgWrapperHref);

    // Show loadin image before image is loaded
    loadingImg.appendTo('#fancybox__img-wrapper');

    // Image will show after it will be loaded, before loading image is showed
    image.on('load', function() {
        image.appendTo('#fancybox__img-wrapper');
        loadingImg.hide(); // Hide loading image afet image was loaded
    });

    // Make images slider for thumbnails from gallery
    if (thumbWrapper.length == 0) { // add thumbs just if it was not already added

        // Get id of current item
        var itemId = $('.item-detail').attr('id');

        // LOAD photos data to object
        $.ajax({
            url: '_inc/getPhotosFromDb.php',
            type: 'post',
            data: {
                item_id: itemId
            }
        }).done(function(html) {
            galleryImgs = $(html);

            galleryImgs.each(function() {

                var src = $(this).attr('src'),
                    href = src.replace('thumbs/','');
                    thumbWrapper = $('<a/>', {id: 'fancybox__img-thumb-wrapper'}),
                    thumbImg = $('<img>', {id: 'fancybox__img-thumb'});

                // Add thumb image src and id from original image and href to a element from original e element in gallery
                thumbWrapper.attr('href', href);
                thumbImg.attr('src', src);
                thumbWrapper.html(thumbImg); // Put image into thumbImgWrapper
                thumbWrapper.appendTo('#fancybox__thumb-slider');

                // If src of thumb and main image is the same then mark thumb as active
                if ((thumbImg.attr('src').substring(thumbImg.attr('src').lastIndexOf("/") + 1) == image.attr('src').substring(image.attr('src').lastIndexOf("/") + 1))) {
                    thumbWrapper.addClass('img-active');
                }
            })
        });
    }
});

我想把这段代码放在开始,但即使我使用$(document).find(...)它甚至找不到它。

// If src of thumb and main image is the same then mark thumb as active
if (($(document).find('#fancybox__img-thumb').attr('src').substring($(document).find('#fancybox__img-thumb').attr('src').lastIndexOf("/") + 1) == image.attr('src').substring(image.attr('src').lastIndexOf("/") + 1))) {
    thumbWrapper.addClass('img-active');
}

ukqbszuj

ukqbszuj1#

在Ouroborus的评论后,我找到了解决方案:

// Create FANCYBOX

    // Show fancybox after click on a elemet of gallery
    gallery.find('a').on('click', function(event) {
        event.preventDefault();

        // Show fancybox
        overlay.appendTo('body');

        // Define variables
        var galleryImgWrapperHref = $(this).attr('href');

        // Show chosen image in fancybox
        image.attr('src', galleryImgWrapperHref);

        // Show loadin image before image is loaded
        loadingImg.appendTo('#fancybox__img-wrapper');

        // Image will show after it will be loaded, before loading image is showed
        image.on('load', function() {
            image.appendTo('#fancybox__img-wrapper');
            loadingImg.hide(); // Hide loading image afet image was loaded
        });

        // Make images slider for thumbnails from gallery
        if (thumbWrapper.length == 0) { // add thumbs just if it was not already added

            // Get id of current item
            var itemId = $('.item-detail').attr('id');

            // LOAD photos data to object
            $.ajax({
                url: '_inc/getPhotosFromDb.php',
                type: 'post',
                data: {
                    item_id: itemId
                }
            }).done(function(html) {
                galleryImgs = $(html);

                galleryImgs.each(function() {

                    var src = $(this).attr('src'),
                        href = src.replace('thumbs/','');
                        thumbWrapper = $('<a/>', {class: 'fancybox__img-thumb-wrapper'}),
                        thumbImg = $('<img>', {class: 'fancybox__img-thumb'});

                    // Add thumb image src and id from original image and href to a element from original e element in gallery
                    thumbWrapper.attr('href', href);
                    thumbImg.attr('src', src);
                    thumbWrapper.html(thumbImg); // Put image into thumbImgWrapper
                    thumbWrapper.appendTo('#fancybox__thumb-slider');

                    if ((thumbWrapper.attr('href').substring(thumbWrapper.attr('href').lastIndexOf("/") + 1) == image.attr('src').substring(image.attr('src').lastIndexOf("/") + 1))) {
                        thumbWrapper.addClass('img-active');
                    }
                })
            })
        }
        var bottomSliderAnchor = $(document).find('#fancybox__thumb-slider a'),
            imageNew = $(document).find('.fancybox__img'),
            bottomSlider = $(document).find('#fancybox__thumb-slider');

        bottomSliderAnchor.each(function() {
            var thumbWrapperNew = $(this);
            // If src of thumb and main image is the same then mark thumb as active
            if ((thumbWrapperNew.attr('href').substring(thumbWrapperNew.attr('href').lastIndexOf("/") + 1) == imageNew.attr('src').substring(imageNew.attr('src').lastIndexOf("/") + 1))) {
                thumbWrapperNew.addClass('img-active');
            }
        })
    });

相关问题