我有以下javascript,如果php脚本下方返回一个高于0的mysql选择计数结果,则应该向用户弹出通知:
<script>
$(document).ready(function() {
function update() {
$.ajax({
type: 'POST',
url: '../ajax/alert/notification.php',
timeout: 1000,
success: function(data) {
$("#notification-alert-main").text(data);
$("#notification-alert-detailed").text(data);
if (data > 0) {
$("#notification-alert-message").innerHTML('<i class="fa fa-warning danger"></i> New alert has been detected');
}
window.setTimeout(update, 5000);
},
});
}
update();
});
</script>
php脚本:
<?php
include('../../db.php');
$queryCountUnreadAlerts = $bdd->prepare("SELECT COUNT(*) Nb FROM exp_alert WHERE ale_read = 0;");
$queryCountUnreadAlerts->execute();
$dataCountUnreadAlerts = $queryCountUnreadAlerts->fetch();
$queryCountUnreadAlerts->closeCursor();
echo(intval($dataCountUnreadAlerts['Nb']));
?>
网络浏览器报告错误:
[Error] TypeError: 'undefined' is not a function (evaluating '$("#notification-alert-message").innerHTML('<i class="fa fa-warning danger"></i> New alert has been detected')')
success (index.php, line 230)
l (jquery.min.js, line 4)
fireWith (jquery.min.js, line 4)
k (jquery.min.js, line 6)
(anonymous function) (jquery.min.js, line 6)
我怀疑是一个错误的变量类型,但我仍然在使用整数。
答案 0 :(得分:3)
使用.html()
代替innerHTML
或者
按以下方式更改
$("#notification-alert-message").get(0).innerHTML="<i class='fa fa-warning danger'></i> New alert has been detected";
答案 1 :(得分:2)
在JQuery中,您使用的是.html()
,而不是.innerHTML