为什么按钮“关闭”不关闭窗口?

时间:2019-08-10 23:57:46

标签: javascript html

我的菜单中有下一个用于打开窗口的代码:

            <li class="menu-item navigation-item menu_auth">
                <a onclick="modal(859,'free-coins')" class="free-coins-link has-free-coins">
                    <div class="image free-coins"></div>
                    <div class="text">text1</div>
                    <div class="free-coins-active">+<span class="value">10</span></div>
                </a>
            </li>

弹出框:

 <div class="popup_block" id="free-coins">
     <div class="free-coins-modal">
         <div class="free-coins-item daily-bonus">
             <div class="header">text1</div>
             <div class="free-coins-sprite daily-bonus"></div>
             <div class="free-coins-text">
                 text1
             </div>
             <div class="btn-yellow free-coins-btn btn-get-daily-bonus" onclick="getBonus()">text1</div>
             <div class="error"></div>
         </div><div class="free-coins-item referral">
             <div class="header">text1</div>
             <div class="free-coins-sprite referral"></div>
             <div class="free-coins-text">text1</div>
             <a onclick="redirect('#/referral')" class="btn-blue referral-btn free-coins-btn disabled">text1</a>
         </div><div class="free-coins-item giveaway">
             <div class="header">text1</div>
             <div class="free-coins-sprite giveaway"></div>
             <div class="free-coins-text">text1</div>
             <a onclick="redirect('#/giveaway')" class="btn-blue free-coins-btn btn-get-tasks disabled">text1</a>
         </div>
     </div>
 </div>

窗口已显示,但没有关闭。我有一个CSS样式,带有关闭按钮和站点JS中的JS代码:


function modal(id,name) {
        var popID = name;
        $('#' + popID).fadeIn().css({
            'width': id
        }).prepend('<a onclick="modalclose" title="Close" class="close"></a>');
        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;
        $('#' + popID).css({
            'margin-top': -popMargTop,
            'margin-left': -popMargLeft
        });
        $('body').append('<div id="fade"></div>');
        $('#fade').fadeIn();
        return false;
}
function modalclose() {
$('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();  
    });
}

但是我无法通过按Esc关闭窗口,也无法通过按“关闭”按钮关闭。我该如何解决?

1 个答案:

答案 0 :(得分:0)

@如何在边界区域添加关闭模式onclick的功能:

window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}