简体   繁体   中英

echo <tr> with a href inside not working

I'm pretty confused about the fact that I have this code:

    while($row = mysql_fetch_array($result)) {
       echo "<tr href='http://google.com'>";
       echo "<td>" . $row['rowname'] . "</td>";
       echo "<td>" . $row['rowname2'] . "</td>";
       echo "<td>" . $row['rowname3'] . "</td>";
       echo "<td>" . $row['rowname4'] . "</td>";
       echo "</tr>";
    }

and for some reason my link doesn't work. I've tried putting a tag around the tr but with no success. Somebody to have a clue?

Guess you are looking for link to entire row. This can be achieved by having onClick function on the row, instead of using href .

<tr onClick="location.href='target url'">
   <td></td>
    <td></td>
 </tr>
while($row = mysql_fetch_array($result)) {
   echo "<tr onclick='window.location.href = \"http://google.com\";'>";
   echo "<td>" . $row['rowname'] . "</td>";
   echo "<td>" . $row['rowname2'] . "</td>";
   echo "<td>" . $row['rowname3'] . "</td>";
   echo "<td>" . $row['rowname4'] . "</td>";
   echo "</tr>";
}

If you want to make entire row clickable and work like an a tag you should use javascript for this:

echo "<tr onclick='location.href = \"http://google.com\"'>";

And then it would be nice to add CSS cursor pointer to clickable table cells:

tr td {
    cursor: pointer;
}

a tr-tag has no href attribute, see here .

you can do something like:

<tr>
    <td>
       <a href="google.com">Google</a>
    </td>
    ....
</tr>

Another Solution would be adding javascript if the whole row should be clickable:

<tr onclick="window.location.href = google.com">
    <td>
       <a href="google.com"</a>
    </td>
    ....
</tr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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