dsaj20411 2016-02-20 17:34
浏览 45
已采纳

ajax关闭一次后才重新开放

Im using ajax with wordpress to open post's as a modal. Its not a php problem but jquery so im asking here. Modal is display:none as default. When you click on a wordpress link modal opens (display:block) and loads a post. This works. Then when you click on close button, modal goes to display:none again. This works as expected. The problem is when i try to re-open the link, or any other post link, nothing happens, only overlay (overlay div is behind modal for effect) is showed. No modal or link's html is loaded. The modal is not even shown as blank.

jquery

jQuery(document).ready(function ($) {

            $.ajaxSetup({cache: false});

            var post_link = $(".post-link").attr("href");

            $(".post-link").click(function () {
                $(".ajax-cont").addClass("ajax-popup-show");
                $("#single-post-container").addClass("ajax-popup-show").html("Molimo Pričekajte");
                $("#single-post-container").load(post_link);
                $(".ovrly").fadeIn(100);
                return false;
            });

            $(".ajax-close").click(function () {
                $(".ajax-cont").empty().addClass("ajax-popup-hide");
                $("#single-post-container").empty().addClass("ajax-popup-hide");
                $(".ovrly").fadeOut(100);
            });

        });

CSS

.ajax-cont {
    position: absolute;
    width: 75%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    z-index: 999;
    height: auto;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    border-radius: 2px;
    -moz-background-clip: padding;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    top: 10%;
    display: none;
    left: 50%;
}

.prodaja-cont {
    position: absolute;
    width: 40%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    z-index: 999;
    height: auto;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    border-radius: 2px;
    -moz-background-clip: padding;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    top: 10%;
    display: none;
    left: 50%;
}

.ajax-popup {
    width: 100%;
    padding: 30px;
    background: #fff;
}

.ajax-close {
    position: absolute;
    right: 30px;
    top: 0;
    cursor: pointer;
    z-index: 9999;
    width: 10px;
    height: 10px;
}

.ajax-close:before {
    content: "\f404";
    font-family: "Ionicons";
    font-size: 3em;
}

.ajax-popup-show {
    display: block;
}

.ajax-popup-hide {
    display: none;
}

.ovrly {
    position: fixed;
    top: 0;
    left: 0;
    overflow: auto;
    width: 100%;
    height: 100%;
    min-width: 100%;
    min-height: 100%;
    z-index: 10;
    background-color: rgba(0, 0, 0, 0.5);
    /*dim the background*/
    display: none;
}

HTML/PHP

<div class="ovrly"></div>
    <div class="ajax-cont">
        <div class="ajax-popup" id="single-post-container"></div>
        <div class="ajax-close"></div>
    </div>

<a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  • 写回答

1条回答 默认 最新

  • dongmu5815 2016-02-20 17:42
    关注

    I think that when $(".ajax-cont").empty() is executed, all its content is removed, including

    <div class="ajax-popup" id="single-post-container"</div>
    <div class="ajax-close"></div>
    

    Try changing first line of close handler to $(".ajax-cont").addClass("ajax-popup-hide") removing the empty()

    The second time you click on the link, there isn't any #single-post-container where put you content.

    You should also remove class ajax-popup-hide from both .ajax-cont and #single-post-container

    $(".post-link").click(function () {
      $(".ajax-cont").addClass("ajax-popup-show").removeClass("ajax-popup-hide");
      $("#single-post-container").addClass("ajax-popup-show").removeClass("ajax-popup-hide").html("Molimo Pričekajte");
      $("#single-post-container").load(post_link);
      $(".ovrly").fadeIn(100);
      return false;
    });
    
    $(".ajax-close").click(function () {
      $(".ajax-cont").addClass("ajax-popup-hide");
      $("#single-post-container").empty().addClass("ajax-popup-hide");
      $(".ovrly").fadeOut(100);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大