简体   繁体   English

从“this”关键字获取属性时,在 javascript 中迭代双“for-each”function 返回未定义

[英]Iterating over double 'for-each' function in javascript returning undefined when getting property from 'this' keyword

This must be a stupid question, but I am not able to figure anything to do about it.这一定是一个愚蠢的问题,但我无法解决任何问题。

I have a simple HTML table with every element having a property 'data-value'.我有一个简单的 HTML 表,每个元素都有一个属性“数据值”。 I need to do some operations based on that, but one column at a time.我需要在此基础上进行一些操作,但一次一列。 Hence, I wrote a function which only runs on the first td element of each row.因此,我写了一个 function ,它只在每行的第一个 td 元素上运行。 I am getting the rows by running another for each function.我通过为每个 function 运行另一个来获取行。

When I use console.log($(this)) , I am getting the expected td element from the all the first columns.当我使用console.log($(this))时,我从所有第一列中获得了预期的 td 元素。 Now, I want to store the 'data-value' property in another variable and do stuff on it.现在,我想将“数据值”属性存储在另一个变量中并对其进行处理。

  $(this).find('td:first-of-type').each(function() {
    console.log($(this));
    //returns td object
  })
});

As seen here, all the td elements are correctly being logged on console.如此处所示,所有 td 元素都正确登录到控制台。

正确工作

But when I use jquery to get the property 'data-value' from it, I get undefined.但是当我使用 jquery 从中获取属性“数据值”时,我得到了未定义。 不工作

What could be the error?可能是什么错误? Is this the best way to iterate every column's tds property?这是迭代每列的 tds 属性的最佳方法吗?

 $('#table_canvas tbody tr').each(function() { $(this).find('td:first-of-type').each(function() { console.log($(this)); //returns td object }) }); $('#table_canvas tbody tr').each(function() { $(this).find('td:first-of-type').each(function() { console.log($(this).attr('data-value')); //returns tr object which does not have the 'data-value'property }) });
 table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> <h2>HTML Table</h2> <table id="table_canvas" class="table table--bordered" style="width: 100%; height: 100%;"> <thead> <tr style="text-align: right;"> <th></th> <th>first_name</th> <th>is_staff</th> <th>last_name</th> </tr> <tr> <th>id</th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <th hello="hi">1</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>3</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>4</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>5</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>6</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>7</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> </tbody> </table>

its been awhile i dont use javascript but i think this is work for you vanilla js有一段时间我不使用 javascript 但我认为这对你有用 vanilla js

just select every second child of every tr tag仅 select 每个 tr 标签的每个第二个孩子

document.querySelectorAll('#table_canvas tr td:nth-child(2)').forEach( x => {
    console.log(x.dataset.property);
})

Here's a simple way to obtain the data, using vanilla JavaScript:这是获取数据的简单方法,使用 vanilla JavaScript:

  1. form array of <tr> elements形成<tr>元素的数组
  2. map tr array to array of td data-property values map tr 数组到 td data-property值数组

 // array of <tr> elements let trs = Array.from(document.querySelectorAll('#table_canvas tbody tr')); // array of 'data-property' values let props = trs.map(tr => tr.querySelector('td').dataset.property); console.log('props:', JSON.stringify(props));
 table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; }
 <table id="table_canvas" class="table table--bordered" style="width: 100%; height: 100%;"> <thead> <tr style="text-align: right;"> <th></th> <th>first_name</th> <th>is_staff</th> <th>last_name</th> </tr> <tr> <th>id</th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <th hello="hi">1</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>3</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>4</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>5</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>6</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> <tr> <th>7</th> <td data-property="1">1</td> <td data-property="True">True</td> <td data-property="1">1</td> </tr> </tbody> </table>

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

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