如何给 Table/tr/td 添加超链接?

<tr  onclick="location.href={{url('路径')}};"> </tr>
<td  onclick="location.href={{url('路径')}};"> </td>
//在新窗口中打开
<tr onclick="window.open('http://www.baidu.com');">  </tr>
<td onclick="window.open('http://www.baidu.com');">  </td>
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 3

虽然你可以这么做,但是在目前语义化HTML面前,总感觉怪怪的。

4年前 评论
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Test</title>
</head>
<body>
<table border="1">
  <tr>
    <th data-url="http://A.test">A</th>
    <th data-url="http://B.test">B</th>
  </tr>
  <tr>
    <td data-url="http://1.test">1</td>
    <td data-url="http://2.test">2</td>
  </tr>
</table>
<script>
  var ths = document.getElementsByTagName('table');

  ths[0].addEventListener('click', function(event) {
    //TODO 
    console.log(event.target.dataset.url)
  })
</script>
</body>
</html>

这样如何?

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!