gpt4 book ai didi

javascript - 使用 getElementById 的多个元素

转载 作者:行者123 更新时间:2023-11-29 07:33:16 25 4
gpt4 key购买 nike

我希望有人可以帮助我,我想做的是在多个<div>中显示一个java脚本元素。从 while 循环生成(来自 mysql)。

我正在使用getElementById但我知道getElementById元素仅获取第一个 <div> - 我不太确定如何让它适用于所有人<div> s。

这是迄今为止我的代码:

    <?php
session_start();

$server = '...';
$user = '...';
$pass = '...';
$db = '...';

// Connect to Database
$result = mysql_connect($server, $user, $pass)
or die ("Could not connect to server ... \n" . mysql_error ());
mysql_select_db($db)
or die ("Could not connect to database ... \n" . mysql_error ());


// No session variable, red from mysql
$result=mysql_query("select * from players");
$time=mysql_fetch_array($result);
$dateFormat = "d F Y -- g:i a";
$targetDate = strtotime($time['time']);
$_SESSION['targetDate'] = $targetDate;



$actualDate = time();
$secondsDiff = $actualDate - $targetDate;
$remainingDay = floor($secondsDiff/60/60/24);
$remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)- ($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)- ($remainingHour*60*60))-($remainingMinutes*60));
$actualDateDisplay = date($dateFormat,$actualDate);
$targetDateDisplay = date($dateFormat,$targetDate);

echo $actualDateDisplay;
echo $targetDateDisplay;
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var days = <?php echo $remainingDay; ?>
var hours = <?php echo $remainingHour; ?>
var minutes = <?php echo $remainingMinutes; ?>
var seconds = <?php echo $remainingSeconds; ?>
function setCountDown ()
{
seconds++;
if (seconds > 60){
minutes++;
seconds = 0
}
if (minutes > 60){
hours++;
minutes = 0
}
if (hours > 6){
hours = 0
}
document.getElementsByClassName("remain") = " "+hours+" hr "+minutes+" min "+seconds+" sec";
SD=window.setTimeout( "setCountDown()", 1000 );
if (minutes == '00' && seconds == '00') {
seconds = "00"; window.clearTimeout(SD);
window.location = "result.php"
}

}
</script>

</head>
<body onLoad="setCountDown();">

<?php
while($row = mysql_fetch_array( $result )) {

echo "<div class=\"remain\">";
echo "$remainingHour h, $remainingMinutes min, $remainingSeconds s, ";
echo "</div>";
}

?>

最佳答案

根据 HTML 标准,ID 必须是唯一的:

http://www.w3.org/TR/html401/struct/global.html#h-7.5.2

尝试设置类,通过搜索将返回结果数组。然后您可以迭代这些!

编辑:这是使用 document.getElementsByClassName(); 的一些小技巧 https://jsfiddle.net/965zbf0m/

关于javascript - 使用 getElementById 的多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31843281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com