简体   繁体   English

jQuery-从Ajax检索数据后如何添加关闭按钮?

[英]jQuery - How to add button close after retrieve data from Ajax?

I have a problem with retrieve data from Ajax. 我从Ajax检索数据时遇到问题。 I want to add button "X" to close popup after show data. 我想添加按钮“ X”以关闭显示数据后的弹出窗口。

I tried to add a button like: 我试图添加一个像这样的按钮:

jQuery(".popup").append('<button style="font-size: 150%; color: red; top: 0; right: 0;">x</button>');

in to popup to the user can close my popup. 在弹出窗口中,用户可以关闭我的弹出窗口。

My code like: 我的代码如下:

function getData(domain){
    var dataString = "domain=" + domain + "&security="+ mhdomain.security + "&action=getdomain" ;
    jQuery.ajax({
        action: "getDomain",
        type: "post",
        url: mhdomain.ajaxurl,
        dataType: "json",
        data: dataString,
        beforeSend: function(xhr){
            jQuery("#wrapper").append('<div class="popup" onclick="popupout(\'.popup\')"></div>');
            jQuery(".popup").append("<img src='https://i.imgur.com/CH8XnNt.gif' alt='loading' />");  
            jQuery(".popup").fadeIn();
            jQuery(".popup").append('<button style="font-size: 150%; color: red; top: 0; right: 0;">x</button>');
        },
        success : function(data){
            jQuery(".popup").html(data.data);
        },
    });      
};
function popupout(popup){
    jQuery(popup).fadeOut(1000);
    jQuery(popup).remove();
}

My CSS code: 我的CSS代码:

.popup {
    position: fixed;
    display: none;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 99999;
    max-width: 780px;
    width: 100%;
    max-height: 480px;
    height: 100%;
    display: inline-block;
    cursor: pointer;
    background-color: white;
    padding: 30px 50px;
    color: black;
    overflow-y: scroll;
    border: 1px solid #f2f2f2;
}

In your beforeSend event you are appending the html which is fine. 在您的beforeSend事件中,您可以附加html。 But it gets replaced in success event. 但是在成功事件中它将被替换。 in this line the issue is 在这一行中,问题是

jQuery(".popup").html(data.data);

But instead of you just append data.data here also which will fix the issue. 但是,除了在这里追加data.data之外,还可以解决此问题。 like this 像这样

jQuery(".popup").append(data.data);

For your question in comment, still you can use beforeSend event. 对于您的评论问题,您仍然可以使用beforeSend事件。 Like this 像这样

beforeSend:function(){
// after your codes
jQuery(".popup").append("<img src='loading.gif' class='loading'>");
}

success:function(){
// after your codes
jQuery(".popup").find('.loading').remove();
}

You can tweak the appearance of the images using css. 您可以使用CSS调整图像的外观。 But I shared an idea for how to achieve that 但是我分享了如何实现这一目标的想法

You can add your close button html in success. 您可以成功添加close button html。

 function getData(domain){
    var dataString = "domain=" + domain + "&security="+ mhdomain.security + "&action=getdomain" ;
    jQuery.ajax({
        action: "getDomain",
        type: "post",
        url: mhdomain.ajaxurl,
        dataType: "json",
        data: dataString,
        beforeSend: function(xhr){
            jQuery("#wrapper").append('<div class="popup" onclick="popupout(\'.popup\')"></div>');
            jQuery(".popup").append("<img src='https://i.imgur.com/CH8XnNt.gif' alt='loading' />");  
            jQuery(".popup").fadeIn();
            jQuery(".popup").append('<button style="font-size: 150%; color: red; top: 0; right: 0;">x</button>');
        },
        success : function(data){
            jQuery(".popup").html('<div class="popup" onclick="popupout(\'.popup\')"></div>'+data.data);
        },
    });      
};
function popupout(popup){
    jQuery(popup).fadeOut(1000);
    jQuery(popup).remove();
}

The close button is being overwritten by jQuery(".popup").html(data.data); jQuery(".popup").html(data.data);覆盖了关闭按钮jQuery(".popup").html(data.data); because you are replacing everything inside of .popup . 因为您要替换.popup内的.popup

I have some of your code in the snippet below using .append instead of .html and your button appears and it works. 我在下面的代码段中使用.append而不是.html编写了一些代码,并且您的按钮出现了并且可以正常工作。

I would just put a placeholder in the popup that houses the loading gif and replace that div's html with the data. 我只是在容纳加载gif的弹出窗口中放置一个占位符,然后用数据替换该div的html。

 function getData(){ jQuery("#wrapper").append('<div class="popup" onclick="popupout(\\'.popup\\')"></div>'); jQuery(".popup").append("<img src='https://i.imgur.com/CH8XnNt.gif' alt='loading' />"); jQuery(".popup").fadeIn(); jQuery(".popup").append('<button style="font-size: 150%; color: red; top: 0; right: 0;">x</button>'); jQuery(".popup").append('popup data'); }; function popupout(popup){ jQuery(popup).fadeOut(1000); jQuery(popup).remove(); } getData(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper" ></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM