如何在</span>
之后将AJAX数据放入?
$(function () {
function check_messages() {
$.ajax({
url: '/check/',
cache: false,
success: function (data) {
$('.here').text(data)
complete: function () {
window.setTimeout(check_messages, 20000);
}
});
};
check_messages();
});
<button type="button" class="navbar-toggle collapsed msg" data-toggle="collapse" data-target="#search">
<span class="glyphicon glyphicon-envelope here" aria-hidden="true"></span>
HERE ajax data
</button>
答案 0 :(得分:1)
实际上你的代码段是错误的。例如:complete
回调位于success
回调中,或者可以说最外面的功能并未关闭,具体取决于您想要如何阅读。
这里我们使用包含jQuery after()
method的清理版本来在您喜欢的DOM元素之后添加数据:
$(function () {
function check_messages() {
$.ajax({
url: '/check/',
cache: false,
success: function (data) {
// instead of $('.here').text(data)
$('.here').after(data)
},
complete: function () {
window.setTimeout(check_messages, 20000);
}
});
};
check_messages();
});
编辑:
$(function () {
var $CheckMessages = $('<span/>').insertAfter('.here');
function check_messages() {
$.ajax({
url: '/check/',
cache: false,
success: function (data) {
$CheckMessages.html(data);
// if data is text only (no HTML) it would be more secure to use .text() method...
// $CheckMessages.text(data);
},
complete: function () {
window.setTimeout(check_messages, 20000);
}
});
};
check_messages();
});