刷新具有相同ID

时间:2016-07-03 13:25:53

标签: javascript php jquery

我有多个具有相同ID的<div>个标签。根据日期和时间,I语句触发选择相关内容。

我还会不断监控数据库以获取新内容,并需要刷新<div>以显示更新或新内容。

我尝试了类似$("#myDiv").load('myPHPPage.php?);的内容但没有成功。

我的PHP代码示例:

if($dayofweek == "Monday" && $timeofday >= "08:00:00" && $timeofday <= "11:59:59"){
//Monday morning timeslot
echo "<div id='slideshow'>";
$stmm= $conn->prepare("SELECT `Monday_Morning`
FROM `adsList`");
$stmm->execute();
while($resultst = $stmm-> fetch()){
$mondaymoringlist = $resultst["Monday_Morning"];
echo "<div class='slideshow'>";
//Display ad as an image
//<object data='/$adlist'></object>
echo "<object data='/$mondaymoringlist'></object>";
echo "</div>";
}
echo "</div>";
}
//Monday Afternoon
if ($dayofweek == "Monday" && $timeofday >= "12:00:00" && $timeofday     <= "16:59:59"){
//Monday afternoon timeslot
echo "<div id='slideshow'>";
$stma= $conn->prepare("SELECT `Monday_Afternoon`
FROM `adsList`");
$stma->execute();
while($resultst = $stma-> fetch()){
$mondayafternoonlist = $resultst["Monday_Afternoon"];
echo "<div class='slideshow'>";
//Display ad as an image
//<object data='/$adlist'></object>
echo "<object data='/$mondayafternoonlist'></object>";
echo "</div>";
}
echo "</div>";
}
//Monday Evening

else if ($dayofweek == "Monday" && $timeofday >= "17:00:00" && $timeofday <= "22:00:00"){
//Monday Evevening timeslot
echo "<div id='slideshow'>";
$stme= $conn->prepare("SELECT `Monday_Evening`
FROM `adsList`");
$stme->execute();
while($resultst = $stme-> fetch()){
$mondayeveninglist = $resultst["Monday_Evening"];
echo "<div class='slideshow'>";
//Display ad as an image
//<object data='/$adlist'></object>
echo "<object data='/$mondayeveninglist'></object>";
echo "</div>";
}
echo "</div>";
}
//End of Monday

一周中的每一天都有类似的时间段。

我目前使用location.reload();,但这并不理想,因为页面上还有另一个<div>,它也会根据自己的数据检查单独刷新。使用其他<div>我可以$("#myDiv").load('myPHPPage.php?);,因为它有唯一的ID。

AJAX

function adshandler() {
var d = new Date();
var cache = d.getTime();
 $.getJSON("check_time.php?cache=" + cache, function(update) {
    if (update.count === true) {
        $("#slideshow").load();
    }
  }); 
 } 
 setInterval(adshandler, 600000);

1 个答案:

答案 0 :(得分:1)

ID和className之间的唯一区别是className(aka class)可以与多个元素相关联。根据定义,页面上的每个ID必须是唯一的。

当多个元素具有相同的ID时,只会对页面上的第一个元素(带有该ID)进行更改。

将ID更改为类。

要定期轮询数据库,请使用recursive setTimeoutAJAX code block