jQuery獲取點選td單元格的值

antzone發表於2018-07-18

分享一段程式碼例項,它實現了獲取當前點選td單元格的值的功能。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
$(function () {
  $("#mytable td").click(function () {
    $("#antzone").text($(this).text())
  });
});
</script>
</head>
<body>
<div id="antzone"></div>
<table id="mytable" width="708" cellpadding="5" cellspacing="1">
  <tbody>
    <tr bgcolor="#DEE9F4" id="tr_1">
      <td align="center">日</td>
      <td align="center">一</td>
      <td align="center">二</td>
      <td align="center">三</td>
      <td align="center">四</td>
      <td align="center">五</td>
      <td align="center">六</td>
    </tr>
    <tr bgcolor="#DEE9F4" id="tr_2">
      <td align="center"></td>
      <td align="center"></td>
      <td align="center"></td>
      <td align="center">1</td>
      <td align="center">2</td>
      <td align="center">3</td>
      <td align="center">4</td>
    </tr>
    <tr bgcolor="#DEE9F4" id="tr_3">
      <td align="center">5</td>
      <td align="center">6</td>
      <td align="center">7</td>
      <td align="center">8</td>
      <td align="center">9</td>
      <td align="center" bgcolor="red">10</td>
      <td align="center">11</td>
    </tr>
    <tr bgcolor="#DEE9F4" id="tr_4">
      <td align="center">12</td>
      <td align="center">13</td>
      <td align="center">14</td>
      <td align="center">15</td>
      <td align="center">16</td>
      <td align="center">17</td>
      <td align="center">18</td>
    </tr>
  </tbody>
</table>
</body>
</html>

相關文章