简体   繁体   English

在另一个 td 孩子的表中查找 td

[英]Find td in a table from another td child

Basically i want to find the value of the td element of the class "number" value when i press the input button.基本上,当我按下输入按钮时,我想找到 class“数字”值的 td 元素的值。

<table id="tableid">
 <tbody>
         <tr>
            <td> </td>
            <td class="number"> 5 </td>
            <td> </td>
            <td> </td>
            <td><input type="button" onclick="value(this)"></td>
        </tr>
 </tbody>
</table

This is the javascript which is not working:这是 javascript 不工作:

function value(row) {
    var number = row.parentNode.parentNode.find("td:eq(1)").text(); 
    alert(number)
 }

What i'm missing?我错过了什么?

$(row).closest('tr').find('.number').text();

Use closest to go up to the row, then find the element by its class, and then get its value.使用最接近 go 直到行,然后通过它的 class 找到元素,然后得到它的值。

Hello I assume this is general question and you are not asking for finding dynamically created table.您好,我认为这是一般问题,您不是要求查找动态创建的表。 I suggest you to go classic JAVASCRIPT我建议你 go 经典 JAVASCRIPT

function myFunction() {
  var x = document.getElementsByClassName("number")[0].innerHTML;
  alert(x);
}

Cheers干杯

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

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