Ajax显示消息
Ajax Displaying Message
有一段令人惊叹的代码,它运行得很好,但问题是我总是得到带有相同消息的新div,我如何更改它,使它出现在相同的位置?msgsrv.php文件只是一个echo "Test Message";
,但它将从MySQL数据库中提取数据。所以我得到的是测试消息,然后是下一行的测试消息,我想用新消息代替旧消息。
<html>
<head>
<title>Auction</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">
body{ background:#000;color:#fff;font-size:.9em; }
.msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}
.old{ background-color:#246499;}
.new{ background-color:#3B9957;}
.error{ background-color:#992E36;}
</style>
<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
/* Simple helper to add a div.
type is the name of a CSS class (old/new/error).
msg is the contents of the div */
$("#messages").append(
"<div class='msg "+ type +"'>"+ msg +"</div>"
);
}
function waitForMsg(){
/* This requests the url "msgsrv.php"
When it complete (or errors)*/
$.ajax({
type: "GET",
url: "msgsrv.php",
async: true, /* If set to non-async, browser shows page as "Loading.."*/
cache: false,
timeout:50000, /* Timeout in ms */
success: function(data){ /* called when request to barge.php completes */
addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
setTimeout(
waitForMsg, /* Request next message */
1000 /* ..after 1 seconds */
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg, /* Try again after.. */
15000); /* milliseconds (15seconds) */
}
});
};
$(document).ready(function(){
waitForMsg(); /* Start the inital request */
});
</script>
</head>
<body>
<div id="messages">
<div class="msg old">
Last Bid is:
</div>
</div>
</body>
</html>
正如它所说,"append"将把内容附加到元素中,而.html()将更改元素的整个内部html并替换为您设置的内容。
更换
$("#messages").append(
"<div class='msg "+ type +"'>"+ msg +"</div>"
);
带有:
$("#messages").html(
"<div class='msg "+ type +"'>"+ msg +"</div>"
);
相关文章:
- 如果localstorage为空,则显示欢迎消息
- 消息显示之外的淘汰验证
- ng消息仅在触摸时显示错误,并在错误的初始显示上转换
- 显示服务工作者状态的消息
- 如果用户输入的长度小于最小长度,则显示错误消息
- 当我的条件为true时,显示一条弹出消息
- 为我的rails应用程序显示javascript弹出消息
- javascript文本验证不起作用.警报消息没有't显示
- 如何在asp.net按钮上显示Bootstrap Autocloseable警报消息
- Parsleyjs:如何取消默认消息显示
- 有角度的ng消息显示ng重复表单上的错误
- 为什么我的 JS 代码没有将消息显示到 HTML 页面
- 如何停止 PHP 脚本的响应消息显示为页面的唯一内容
- 错误消息显示在所有分区中
- 警报消息显示我的逻辑是正确的,但谷歌Chrome's的html输出查看器显示不同的内容
- 插件Jquery Validation的错误消息显示不正确
- 鼠标悬停消息显示在图像的某些部分
- 检查文件是否存在,根据消息显示jQuery
- Jquery错误消息显示在每个字段上
- 当错误消息显示用户名和密码不匹配时,如何通过Cognito验证用户池中的用户?