无法从链接打开弹出窗口

时间:2020-07-22 09:53:38

标签: javascript

大家好,我在通过链接从ID调用弹出窗口时遇到了一些问题。它可以点击,但无法通过网址调用,例如exaple.com/#id

(function($) {
$(document).ready(function() {
    //$('a.ajax-post').off('click').on('click', function(e) {
    $("a.ajax-post").on("click", function(e) {
        // show popup 
        $(".popup.supernova").addClass("is-show");

        e.preventDefault(); //Prevent Default Behaviour
        var post_id = $(this).attr("id"); //Get Post ID

        // Ajax Call
        $.ajax({
            cache: false,
            timeout: 8000,
            url: "/wp-admin/admin-ajax.php",
            type: "POST",
            data: {
                action: "theme_post_example",
                id: post_id
            },

            beforeSend: function() {
                $("#ajax-response").html("Loading");
            },

            success: function(data, textStatus, jqXHR) {
                var $ajax_response = $(data);
                $("#ajax-response").html($ajax_response);
                setTimeout(() => {
                    $("#ajax-response").addClass("show-content");
                }, 100);
                /* $(".popup.supernova").append(script); */
            },

            error: function(jqXHR, textStatus, errorThrown) {
                console.log(
                    "The following error occured: " + textStatus,
                    errorThrown
                );
            },

            complete: function(jqXHR, textStatus) {},
        });
    });
});
})(jQuery);
<a href="#" id="8" class="catalog__item popup-8 ajax-post">
    <span class="catalog__title">Title</span>
    <img src="image.jpg" alt="" id="img_8">
</a>

谁能告诉我该怎么做?以及为什么它不起作用。我是js新手。预先感谢

1 个答案:

答案 0 :(得分:0)

根据您的问题,您想在 URL 容器#id这样的情况下打开弹出窗口 exaple.com/#id

您检查URL是否包含#id,然后点击定位标记以编程方式