简体   繁体   English

使表行TR成为链接

[英]Make table row TR a link

I have a table made in PHP with the echo command because it's to make a calendar. 我有一个使用echo命令在PHP中创建的表,因为它是用来创建日历的。 I want each row in the calendar to become a link (to select each week). 我希望日历中的每一行都成为一个链接(每周选择一次)。 I know I can use JavaScript but it wont work when it's in an echo command for some reason. 我知道我可以使用JavaScript,但由于某些原因它在echo命令中时它不会工作。 Is there another way to do this? 还有另一种方法吗?

BTW: I don't want the text to become links just all the cells in the row to become links. 顺便说一句:我不希望文本成为链接中的所有单元格成为链接。

PLEASE let me know if this is possible or what the alternatives are. 如果可能或替代方案是什么,请告诉我。

here is my code I have so far. 这是我到目前为止的代码。

<style style="text/css">
    .hoverTable{
        width:100%; 
        border-collapse:collapse; 
    }
    .hoverTable td{ 
        padding:7px; border:#4e95f4 1px solid;
    }
    /* Define the default color for all the table rows */
    .hoverTable tr{
        background: #b8d1f3;
    }
    /* Define the hover highlight color for the table row */
    .hoverTable tr:hover {
          background-color: #ffff99;
    }
h3 {
    color: #FFF;
}
</style>

.

<table width="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="8" align="center" bgcolor="#666666"><h3>January</h3></td>
      </tr>
      <tr>
        <td width="30" align="center" bgcolor="#0099FF">W</td>
        <td width="30" align="center" bgcolor="#0099FF">S</td>
        <td width="30" align="center" bgcolor="#0099FF">M</td>
        <td width="30" align="center" bgcolor="#0099FF">T</td>
        <td width="30" align="center" bgcolor="#0099FF">W</td>
        <td width="30" align="center" bgcolor="#0099FF">T</td>
        <td width="30" align="center" bgcolor="#0099FF">F</td>
        <td width="30" align="center" bgcolor="#0099FF">S</td>
      </tr>

      <?php 
                    $timestamp = mktime(0,0,0,1,1,$year);
                    $maxday = date("t",$timestamp);
                    $thismonth = getdate ($timestamp);
                    $startday = $thismonth['wday'];
                    $week =  date("W", $timestamp);

            echo "<table class='hoverTable'>";
            for ($i=0; $i<($maxday+$startday); $i++) {

                $date  = mktime(0, 0, 0, 1, $i - $startday + 1, $year);

                            //want to make this row below a link

                if(($i % 7) == 0 ) echo "<tr><td width='30'>" . date('W', $date) . "</a></td>";

                if($i < $startday) echo "<td></td>";
                 else echo "<td align='center' valign='middle' height='20px' width='30px'>". ($i - $startday + 1) . "</td>";

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

?>

So I don't think it's possible to make the cell a link but not the text, but you can make it look like that is happening. 所以我认为不可能让单元格成为链接而不是文本,但是你可以让它看起来像是发生了。 How? 怎么样?

  1. Add an tag as the main element of the td in which all other content is contained 添加标记作为包含所有其他内容的td的主要元素
  2. Make the take up the entire height and width of the td 占据td的整个高度和宽度
  3. Add text-decoration: none to the so the text within won't seem like links 添加text-decoration:none,因此内部的文本看起来不像链接

Code: 码:

<td>
    <a  href="http://www.joshuakissoon.com" title="Joshua Kissoon." style="line-height: 100%; text-decoration: none; width:100%; height:100%">Checkout Joshua Kissoon.</a>
</td>

JSFiddle: http://jsfiddle.net/KrRzP/5/ JSFiddle: http//jsfiddle.net/KrRzP/5/

I would take the approach of styling the a tag to fill the entire td . 我想借此定型的方法a标签来填满整个td

a {
  display: block;
  height: 100%;
  width: 100%;
}

td {
  padding: 0;
}

Example: http://jsfiddle.net/a2w5w/ 示例: http//jsfiddle.net/a2w5w/

You can use jquery 你可以使用jquery

$(document).ready(function(){
    $(".calander tr").click(function() {
        $(".calander tr").removeClass("redColor");
        $(this).addClass("redColor");
    });
});

JSFiddle : http://jsfiddle.net/M94HE/ JSFiddle: http//jsfiddle.net/M94HE/

Update: 更新:

If I understood your question following is your answer 如果我理解你的问题,那就是你的答案

http://jsfiddle.net/Z3sjL/ http://jsfiddle.net/Z3sjL/

You can just wrap your cell in an anchor if you make the anchor a block level element. 如果将锚点设置为块级元素,则可以将单元格包装在锚点中。 Or you can use event attributes, jquery, or javascript 或者您可以使用事件属性,jquery或javascript

event attributes 事件属性

<table>
    <tr>
        <td onclick="window.location = 'index.html';">Click me!</td>
    </tr>
</table>

a little more.. 再来一点..

<table>
    <tr>
        <td style="cursor:pointer" 
            onMouseover="window.status='http://www.stackoverflow.com/'" 
            onMouseout="window.status=''" 
            onMouseup="window.location='http://www.stackoverflow.com/'">
                Click me!
        </td>
    </tr>
</table>

$("tr").click(function(){
});

$('tr').bind('click', function(){
    window.location = 'http://stackoverflow.com';
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM