单击 10 次后 JavaScript 弹出窗口无法显示

标签 javascript ajax popupwindow

我有一个 JavaScript 弹出窗口,可以正常工作 10 次,然后停止显示。意味着当我单击名称链接时,它显示正常。第二次效果很好。第十一次尝试时,它未能弹出。它没有显示出来。如果我刷新页面并再次单击,它会显示正常 10 次。看来是缓存问题。

function toggle(div_id) {
    var el = document.getElementById(div_id);
    if ( el.style.display == 'none' ) { el.style.display = 'block';}
    else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    popUpDiv_height=blanket_height/2-200;//100 is half popup's height
    popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    window_width=window_width/2-250;//250 is half popup's width
    popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
    blanket_size(windowname);
    window_pos(windowname);
    toggle('blanket');
    toggle(windowname);
}
function doAjaxGet(alertId) {
    $.ajax({
            type : "GET",
            url : "alerts/alertNotification.htm",
            dataType : 'json',
            data : "alertId=" + alertId,
            success : function(response) {
            $("#popUpDiv").remove();
            $("#popUpDiv").append("<table>");
            $("#popUpDiv").append("<tr><th>ID</th><th>Event Type</th><th>Process Name</th><th>Status</th><th>Notification Sent</th><th>Text</th></tr>");
            $("#popUpDiv").append("<tr><td>" + response.alertId + "</td><td>"   + response.eventTypeName + "</td><td>"  + response.processName + "</td><td>"    + response.status + "</td><td>" + response.notificationSent + "</td><td>" + response.comments + "</td></tr>");
            $("#popUpDiv").append("</table>");
            popup('popUpDiv');

    },
    error : function(e) {
        alert('Error: ' + e);
    }
});
}

$(document).ready(function() {
      // Bind click event to a link

      // Cancel the mouseup event in the popup
      $("#popUpDiv").mouseup(function() {
        return false;
      });
      // Bind mouseup event to all the document
      $(document).mouseup(function(e) {
        // Check if the click is outside the popup
        if($(e.target).parents("#popUpDiv").length==0 && !$(e.target).is("#popUpDiv")) {
          // Hide the popup
          $("#popUpDiv").slideUp("fast");
          $("#blanket").slideUp("fast");
        }
      });
});

我的html是:

<td align="center"><input type="hidden" id="alertName" name="alertName${count}" value="${alert.alertName}" /> 

<label><a onclick="doAjaxGet(${alert.id})" href="#"><c:out value="${alert.alertName}"></c:out></a></label>

<div style="display: none;" id="blanket"></div>
<div style="display: none; background: none;" id="popUpDiv"></div>
</td>

最佳答案

问题出在 doAjaxGet 方法中的 $("#popUpDiv").remove(); 。它完全删除了 div,并且 blanket_size(popUpDivVar) 使 div 为 null。我使用 $("#popUpDiv").empty(); 代替,效果很好。

关于单击 10 次后 JavaScript 弹出窗口无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20145354/

相关文章:

android - 关注弹出窗口中的 EditText 会使 Activity 进入休眠状态

javascript - 从 header 响应 native 调用屏幕功能

javascript - 如何在pdfmake中获取完整的网页pdf?

javascript - 如何比较集合中的多个值?

jquery - CSS 样式不适用于 jquery 弹出窗口

Javascript自动关闭弹出窗口

javascript - 为什么这个脚本返回 ' ' null'不是对象'?

c# - Ajax 完整功能问题

javascript - 在codeigniter中使用ajax添加数据后更新select

javascript - AJAX 页面重定向正确。但由于重定向传递的数据要刷新