简体   繁体   English

如何获取a的文本<td>另一个元素<td>带有 jquery 的元素

[英]How to obtain text of a <td> element from another <td> element with jquery

I am trying to use a button inside a table division to set a variable as the same value as another division in the same row, but whenever I run my code (below), it returns the value of all the table divisions concatenated together.我正在尝试使用表分区内的按钮将变量设置为与同一行中的另一个分区相同的值,但是每当我运行我的代码(如下)时,它都会返回连接在一起的所有表分区的值。 I am unsure why this was happening, so I replaced '.children()' with 'childnodes[0]' to try and get only the first name, but this just doesn't work and I don't why.我不确定为什么会发生这种情况,所以我用 'childnodes[0]' 替换了 '.children()' 以尝试只获取名字,但这不起作用,我不知道为什么。

My html looks like this:我的 html 看起来像这样:

  <table>
    <tr>
      <td>John</td>
      <td>Doe</td>
      <td><button>Get First Name</button></td>
    </tr>
  </table>

And my Javascript is this:我的 Javascript 是这样的:

$(document).ready(function(){
    $("button").click(function(){
      var first = $(this).closest("tr").childNodes[0].text();
      alert(first)
        })
    });

You could use class identifiers to get information you need as well.您也可以使用类标识符来获取您需要的信息。

<table>
  <tr>
    <td><span class="first-name">John</span></td>
    <td><span class="last-name">Doe</span></td>
    <td>
      <button class="btn-get-data" data-class="first-name">Get First Name</button>
      <button class="btn-get-data" data-class="last-name">Get Last Name</button>
    </td>
  </tr>
</table>


$(document).ready(function() {
  $(".btn-get-data").click(function() {
    $btn = $(this);
    $tr = $btn.closest('tr');
    var first = $tr.find('.' + $btn.attr('data-class')).html();
    alert(first);
  })
});

If you make the button click generic like so, you can add additional buttons on the page and use that to get the class within that row.如果像这样使按钮单击通用,则可以在页面上添加其他按钮并使用它来获取该行中的类。

Here is a working fiddle example: https://jsfiddle.net/b1r0nucq/这是一个工作小提琴示例: https : //jsfiddle.net/b1r0nucq/

you could find the :first child and get his html() , as below:您可以找到:first child 并获取他的html() ,如下所示:

 $(document).ready(function() { $("button").click(function() { var first = $(this).closest("tr").children(":first").html(); alert(first) }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td>John</td> <td>Doe</td> <td><button>Get First Name</button></td> </tr> </table>

set a variable as the same value as another division in the same row将变量设置为与同一行中的另一个分区相同的值

there are lots of possibilities for this, here are some (with the most useful first (opinion based))这有很多可能性,这里有一些(最有用的第一个(基于意见))

$("button").click(function() {
  var first = $(this).closest("tr").find("td:first").text();
  var first = $(this).closest("tr").find("td").first().text();
  var first = $(this).closest("tr").find("td").eq(0).text();

  var first = $(this).closest("tr").children().first().text();
  var first = $(this).closest("tr").children().eq(0).text();

  var first = $(this).closest("td").siblings().first().text();
});

it returns the value of all the table cells concatenated together它返回连接在一起的所有表格单元格的值

https://api.jquery.com/text https://api.jquery.com/text

Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.获取匹配元素集合中每个元素的组合文本内容,包括其后代,或设置匹配元素的文本内容。

because you're passing the "tr" to text() it gets the text of all the cells (tds) and their content etc and combines them as one, so you need to limit to the first as you've attempted.因为您将“tr”传递给 text() 它获取所有单元格(tds)及其内容等的文本并将它们组合为一个,因此您需要在尝试时限制为第一个。

however .childNodes[0] can only be applied to a DOM element/node, while $(this).closest("tr") gives you a jquery object/collection, which doesn't have .childNodes property.然而.childNodes[0]只能应用于 DOM 元素/节点,而$(this).closest("tr")给你一个 jquery 对象/集合,它没有.childNodes属性。

So the jquery equivalent would be to use .children().eq(0) .因此,jquery 等效项将使用.children().eq(0)

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

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