0

我的桌子是这样的:

<table class="tl">
    <tbody>
        <tr class="tlr">
            <td><a href="link here" title="title here">abcd</a></td>
            <td class="tli"><img src="img src here" alt="English" class="icon"></td>
            <td>I WANT THIS TEXT</td>
            <td class="sn">12</td>
            <td class="ln">44</td>
            <td><div class="r0"></div></td>
        </tr>
    </tbody>
</table>

我正在尝试访问 3rd <td>( <td>I WANT THIS TEXT</td>) 的明文。

我很困惑如何得到这个。请帮我

4

3 回答 3

2

var text = $('.tlr td:nth-child(3)').text();
alert(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tl">
    <tbody>
        <tr class="tlr">
            <td><a href="link here" title="title here">abcd</a></td>
            <td class="tli"><img src="img src here" alt="English" class="icon"></td>
            <td>I WANT THIS TEXT</td>
            <td class="sn">12</td>
            <td class="ln">44</td>
            <td><div class="r0"></div></td>
        </tr>
    </tbody>
</table>

于 2016-08-10T13:49:30.263 回答
0

以下代码用于通过单击任何行获取所有 td 数据。

 var table = document.getElementsByTagName("table")[0];
 var tbody = table.getElementsByTagName("tbody")[0];
 tbody.onclick = function (e) {
     e = e || window.event;
     var data = [];
     var target = e.srcElement || e.target;
     while (target && target.nodeName !== "TR") {
         target = target.parentNode;
     }
     if (target) {
         var cells = target.getElementsByTagName("td");
         for (var i = 2; i < 3; i++) {
             data.push(cells[i].innerHTML);
         }
     }
 };
于 2016-08-10T14:00:15.007 回答
0

Jeet,你需要使用Jquery Selectors ... 看看下面的代码:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ready demo</title>
  <style>
  p {
    color: red;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script>

  $( document ).ready(function() {
    alert($( "table tbody tr td:nth-child(3)" ).text());
  });

  </script>

</head>
<body>


<table class="tl">
    <tbody>
        <tr class="tlr">
            <td><a href="link here" title="title here">abcd</a></td>
            <td class="tli"><img src="https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg" alt="English" class="icon"></td>
            <td>I WANT THIS TEXT</td>
            <td class="sn">12</td>
            <td class="ln">44</td>
            <td><div class="r0"></div></td>
        </tr>
    </tbody>
</table> 

</body>
</html>

希望这可以帮助...

于 2016-08-10T14:03:18.703 回答