2

我用 PHP 制作了一个日历,现在我试图将<td></td>可点击的链接设置为链接,而不是像现在这样在其中包含文本

else echo "<td align='center' 
               valign='middle' 
               height='20px' 
               style='background-color:gray;'>

                    <a href='days.php?day=". ($i - $startday + 1) . "&month=$cMonth&year=$cYear'>".
                         ($i - $startday + 1).
                    "</a>
           </td>";

像这样的工作:

<td onclick="document.location = 'link.php';">&nbsp;</td>

但我不能让它在日历中工作

这是网站上的所有代码:

<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",         "August", "September", "October", "November", "December");
?>
<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
?>
<?php
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
$next_month = 1;
$next_year = $cYear + 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Timesedler</title>
<link href="Connections/small-device.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" class="index_logind">
<tr>
<td colspan="5" align="center"><strong><a href="home.php">T i m e s e d l e r</a>    </strong></td>
</tr>
<tr >
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'tfbruger.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Tilføj Bruger</td>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'sebrugere.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Se Brugere</td>
</tr>
<tr>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'tftimer.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Tilføj Timer</td>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'setimer.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Se Timer</td>
</tr>
<tr>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'tfordre.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Tilføj Ordre</td>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'seordre.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Se Ordre</td>
</tr>
<tr>
<td width="40%" align="center" nowrap="nowrap">&nbsp;</td>
<td width="10%" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = '<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">&lt;</td>
<td width="10%" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = '<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">&gt;</td>
<td width="40%" align="center" nowrap="nowrap">&nbsp;</td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="2" class="index_logind">
<tr align="center">
<td colspan="7" bgcolor="#999999"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999"><strong>M</strong></td>
<td align="center" bgcolor="#999999"><strong>T</strong></td>
<td align="center" bgcolor="#999999"><strong>O</strong></td>
<td align="center" bgcolor="#999999"><strong>T</strong></td>
<td align="center" bgcolor="#999999"><strong>F</strong></td>
<td align="center" bgcolor="#999999"><strong>L</strong></td>
<td align="center" bgcolor="#999999"><strong>S</strong></td>
</tr>
<?php 
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday']-1;
$today = date("d");
for ($i=0; $i<($maxday+$startday); $i++) {
     if(($i % 7) == 0 ) echo "<tr>";
     if($i < $startday) echo "<td style='background-color:gray;'></td>";

else if(($i - $startday +1 ==$today) and ($cMonth == date("n")) and ($cYear == date("Y"))) echo "<td align='center' valign='middle' height='20px' style='background-color:#00FF00;'><a href='timer.php?day=". ($i - $startday + 1) . "&month=$cMonth&year=$cYear'>".($i - $startday + 1)."</a></td>";
else echo "<td align='center' valign='middle' height='20px' style='background-color:gray;'><a href='timer.php?day=". ($i - $startday + 1) . "&month=$cMonth&year=$cYear'>".($i - $startday + 1)."</a></td>";

if(($i % 7) == 6 ) echo "</tr>";
}
?>  
</table>
</body>
</html>
4

1 回答 1

0

使用一些 JavaScript 对您的代码进行微小的更改。

<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
?>
<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
?>
<?php
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
$next_month = 1;
$next_year = $cYear + 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Timesedler</title>
<link href="Connections/small-device.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a:link {
    color: #FFF;
    text-decoration: none;
}
a:visited {
    text-decoration: none;
    color: #FFF;
}
a:hover {
    text-decoration: none;
    color: #FFF;
}
a:active {
    text-decoration: none;
    color: #FFF;
}
-->
</style>

<script type="text/javascript">

function addRowHandlers() {
var table = document.getElementById('calander'),
    cells = table.getElementsByTagName('td');

for (var i=0,len=cells.length; i<len; i++){
    cells[i].onclick = function(){
        var cell = this.id;
        if (cell > '') {
        window.location = cell;
        }
        /* if you know it's going to be numeric:
        console.log(parseInt(this.innerHTML),10);
        */
    }
}
}
</script>

</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" class="index_logind">
<tr>
<td colspan="5" align="center"><strong><a href="home.php">T i m e s e d l e r</a>    </strong></td>
</tr>
<tr >
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'tfbruger.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Tilføj Bruger</td>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'sebrugere.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Se Brugere</td>
</tr>
<tr>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'tftimer.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Tilføj Timer</td>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'setimer.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Se Timer</td>
</tr>
<tr>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'tfordre.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Tilføj Ordre</td>
<td width="50%" colspan="2" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = 'seordre.php';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">Se Ordre</td>
</tr>
<tr>
<td width="40%" align="center" nowrap="nowrap">&nbsp;</td>
<td width="10%" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = '<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">&lt;</td>
<td width="10%" align="center" nowrap="nowrap" bgcolor="#EEEEEE" style="border:1px solid black;" onclick="document.location = '<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>';" onMouseover="this.bgColor='#CCCCCC'"onMouseout="this.bgColor='#EEEEEE'">&gt;</td>
<td width="40%" align="center" nowrap="nowrap">&nbsp;</td>
</tr>
</table>

<table id="calander" onclick="addRowHandlers(this.id)" width="200" border="0" align="center" cellpadding="0" cellspacing="2" class="index_logind">
<tr align="center">
<td colspan="7" bgcolor="#999999"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999"><strong style="color: #FFFF00">M</strong></td>
<td align="center" bgcolor="#999999"><strong style="color: #FFFF00">T</strong></td>
<td align="center" bgcolor="#999999"><strong style="color: #FFFF00">W</strong></td>
<td align="center" bgcolor="#999999"><strong style="color: #FFFF00">T</strong></td>
<td align="center" bgcolor="#999999"><strong style="color: #FFFF00">F</strong></td>
<td align="center" bgcolor="#999999"><strong style="color: #FFFF00">S</strong></td>
<td align="center" bgcolor="#999999"><strong style="color: #FFFF00">S</strong></td>
</tr>

<?php 
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday']-1;
$today = date("d");
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>";
if($i < $startday) echo "<td style='background-color:gray;'></td>";

else if(($i - $startday +1 ==$today) and ($cMonth == date("n")) and ($cYear == date("Y"))) echo "<td id='timer.php?day=".($i - $startday + 1)."&month=$cMonth&year=$cYear' align='center' valign='middle' height='20px' style='background-color:#00FF00;'>".($i - $startday + 1)."</td>";
else echo "<td id='timer.php?day=".($i - $startday + 1)."&month=$cMonth&year=$cYear' align='center' valign='middle' height='20px' style='background-color:gray;'>".($i - $startday + 1)."</td>";

if(($i % 7) == 6 ) echo "</tr>";
}
?> 

</table>
</body>
</html>

例子

于 2013-08-31T23:39:01.173 回答