在 ajax 请求后重新加载脚本 (Poptrox)

Reload script (Poptrox) after ajax request

所以我正在使用 AJAX 请求将数据从数据库加载到 DIV。同时,我在不同的脚本 (Poptrox) DIV 中使用相同的数据 做一些设计工作。

没有 AJAX 请求,poptrox 可以工作,但如果我将它与 poptrox 一起使用,它就不再进行设计了。

有没有办法在 ajax 请求后重新加载 poptrox 脚本?

注意:AJAX-代码和 Poptrox-代码位于“main.js”内!

提前致谢!

php:

<script src="assets/js/jquery.min.js"></script>
<section class="wrapper">
    <form action="kochbuch.html" method="get" id="searchfilter">
        <input type="text" class="searchBox" name="searchBox" id="searchBox" placeholder="Search..">
        <button type="submit" id="searchBtn"><i class="fas fa-search"></i></button>
    </form>
</section>
<section class="wrapper">
<div id="gerichte"></div>
</section>
<script src="assets/js/main.js"></script>

js-ajax:

var queryString = window.location.search;
var urlParams = new URLSearchParams(queryString);
if (urlParams.has('searchBox')){
                    var searchBox = urlParams.get('searchBox');
                    $.ajax({
                                    type: "POST",
                                    url: "kochbuch/fetchdata.php",
                                    data: {
                                                    "search_post_btn": 1,
                                                    "searchBox": searchBox,
                                    },
                                    dataType: "json",
                                    success: function (response) {
                                                    $("#gerichte").html(response);
                                    }
                 });
            };

js-poptrox:

// Gerichte.
        var $gerichte = $('#gerichte');

        // Thumbs.
            $gerichte.children('.thumb').each(function() {

                var $this = $(this),
                    $image = $this.find('.image'), $image_img = $image.children('img'),
                    x;

                // No image? Bail.
                    if ($image.length == 0)
                        return;

                // Image.
                // This sets the background of the "image" <span> to the image pointed to by its child
                // <img> (which is then hidden). Gives us way more flexibility.

                    // Set background.
                        $image.css('background-image', 'url(' + $image_img.attr('src') + ')');

                    // Set background position.
                        if (x = $image_img.data('position'))
                            $image.css('background-position', x);

                    // Hide original img.
                        $image_img.hide();

            });


    // Poptrox.
        $gerichte.poptrox({
            baseZIndex: 20000,
            caption: function($a) {

                var s = '';

                $a.nextAll().each(function() {
                    s += this.outerHTML;
                });

                return s;

            },
            fadeSpeed: 300,
            onPopupClose: function() { $body.removeClass('modal-active'); },
            onPopupOpen: function() { $body.addClass('modal-active'); },
            overlayOpacity: 0,
            popupCloserText: '',
            popupHeight: 150,
            popupLoaderText: '',
            popupSpeed: 300,
            popupWidth: 150,
            selector: '.thumb > a.image',
            usePopupCaption: true,
            usePopupCloser: true,
            usePopupDefaultStyling: false,
            usePopupForceClose: true,
            usePopupLoader: true,
            usePopupNav: true,
            windowMargin: 50
        });

        // Hack: Set margins to 0 when 'xsmall' activates.
            breakpoints.on('<=xsmall', function() {
                $main[0]._poptrox.windowMargin = 0;
            });

            breakpoints.on('>xsmall', function() {
                $main[0]._poptrox.windowMargin = 50;
            });

好的,我找到了一种可能的解决方案,但知道预加载的数据是格式化的:

整个 poptrox 代码必须由这个函数包装:

$(document).ajaxStop(function () {

     // function after ajax

});

如果您有更好的解决方案,请随时提交:)

编辑:

好的,我的解决方案如下:

if (urlParams.has('searchBox')){
        var searchBox = urlParams.get('searchBox');
} else {
    var searchBox = '';
}

    $.ajax({ .....