简体   繁体   English

使用jquery向下钻取多个级别(子级和后代)以获取文本

[英]Drilling down many levels (children and decendants) with jquery to get text

trying to drill down and get a name from within a table element. 试图向下钻取并从table元素中获取名称。 i have an id name only for the table. 我只有表的id名称。 what is the best way of drilling down from the great-great nesting element to the desired span , and get its text into an ins ? 什么是从伟大的嵌套元素钻到所需span的最佳方法,并将其文本放入ins

HTML HTML

....<ins id="target"></ins>...

<table id="names">
      <tbody>
      <tr>...</tr>
      <tr>
          <td>..</td>
          <td>..</td>
          <td>
              <img />
              <button>...</button>
              <span>TEXT TO GET</span>

         </td></tr></tbody></table>

Incomplete Jquery: 不完整的Jquery:

$(document).ready(function() {
$("#target").text($("#names>tbody>???DRILLDOWN???).text());
});

If your HTML is not change, then you can use: 如果您的HTML没有更改,那么您可以使用:

$(document).ready(function() {
    $("#target").text($("#names>tbody tr:eq(1) td:eq(2) span").text());
});

But actually, it's fragile if your HTML is not stable. 但实际上,如果您的HTML不稳定,它就很脆弱。 I'd suggest to give your span an id or class to target it more accurately instead. 我建议给你的跨度一个idclass来更准确地定位它。

Try this $("#names").find('span').text(); 试试这个$("#names").find('span').text();

Check demo here Jsfiddle 在这里查看演示Jsfiddle

$("#names tr>td>span").text();

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

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