javascript - HTML5( Bootstrap ): Display "floating" error message

标签 javascript jquery html css twitter-bootstrap

我正忙于创建管理面板。这些管理面板包括各种表格、模式和对话框,并通过 AJAX 提交数据。当服务器遇到错误时,它会将错误作为“数据”发送回 AJAX 请求,然后必须显示该请求。我正在寻找一种方法来在屏幕上显示“ float ”错误消息几秒钟。这必须弹出在屏幕上的所有内容之上(包括打开的模式)。

执行此操作的一种方法是 javascript 警报,但这对用户/移动设备不友好。另一种方法是通过模态/对话框,但如果模态/对话框已经打开,则此方法不起作用。简而言之,我正在寻找一种在屏幕中央显示“ float div”的方法,无论用户滚动到页面上的哪个位置或者当时打开了哪些模式/对话框。

最佳答案

您可以在 ajax 响应回调中创建一个元素:

$.ajax({
  url: "your-script.php",
  done: function(response) {
    // set the message to display: none to fade it in later.
    var message = $('<div class="alert alert-error error-message" style="display: none;">');
    // a close button
    var close = $('<button type="button" class="close" data-dismiss="alert">&times</button>');
    message.append(close); // adding the close button to the message
    message.append(response); // adding the error response to the message
    // add the message element to the body, fadein, wait 3secs, fadeout
    message.appendTo($('body')).fadeIn(300).delay(3000).fadeOut(500);
  }
});

还有 CSS:

.error-message {
    position: fixed;
    top: 45%;
    left: 50%;
    margin-left: -150px;
    width: 300px;
    z-index: 9999;
}

调整 z-index 以将消息置于最前面。

关于javascript - HTML5( Bootstrap ): Display "floating" error message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20971051/

相关文章:

javascript - jquery对话框打开多个窗口

jquery - 获取上传文件数

html - Angular 选项卡的背景颜色更改

jquery - 修复相对于浏览器 Chrome 的 HTML 页面元素

jquery - -webkit-transform 旋转和位置绝对左侧

Javascript - HTML 中的对象循环和组织数据

javascript - 使用 facebook 批量请求 javascript api

javascript - 如何在单选按钮点击功能上添加特定的 id?

asp.net - 如何保存搜索,选择最近的搜索,然后使用 jQuery 和/或 asp.net 填充表单,就像 Priceline 所做的那样

javascript - 如何知道输入间接变化?