在这种情况下,如何将服务器发送的事件用于控制元素显示dom javascript?
我尝试使用dom隐藏服务器发送的事件上的元素,但不起作用,我该怎么办?
test.php
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="1.8.3_test_jquery.minjs.js"></script>
<div id="result"></div>
<div id="hide_this_id">test</div>
<script>
if(typeof(EventSource) !== "undefined")
{
var source = new EventSource("test2.php");
source.onmessage = function(event){
document.getElementById("result").innerHTML += event.data +"<br>";
};
}
else
{
document.getElementById("result").innerHTML = "not support";
}
</script>
</body>
</html>
test2.php
<?php
include("connect.php");
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$loop = "1";
while($loop <= 1)
{
?>
<script>
document.getElementById("hide_this_id").style.display = "none";
</script>
<?PHP
ob_end_flush();
flush();
sleep(5); // wait for 5 seconds
}
?>