It is necessary to count empty lines in the table between text lines.
- you need to select all the rows, select empty lines from the resulting collection, get the length of the selected collection - Grundy
|
3 answers
var table = document.querySelector('table'), tdIndex = 1; var n = 0; // Счётчик пустых // Делаем цикл по всем tr в таблице for(var i = 0, tr = table.querySelectorAll('tr'); i < tr.length; i++){ // Проверяем, пустой ли td if(tr[i].querySelectorAll('td')[tdIndex].innerHTML == ''){ n++; // Прибалвяем 1 к счётчику }; }; console.log(n); // Выводим количество пустых <table border="1"> <tbody> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> </tbody> </table> - doing a search by a selector in a loop condition (
tr = table.querySelectorAll('tr')) is not the best idea since t. performance. it is also better to use the built-intable.rowsselectors instead oftable.querySelectorAll('tr'),table.cells[tdIndex]instead oftable.querySelectorAll('td')[tdIndex]- Sasha Omelchenko
|
// Проверяется ячейка с индексом 1 console.info(Array.from(document.querySelectorAll('table tr')).filter(tr => tr.cells[1].innerHTML.trim() === '').length); <table border="1"> <tbody> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> </tbody> </table> - A beautiful answer, and much better than the first) Good decision! - Vasily Barbashev
- @ Vasily Barbashev, I turned red as my ava :) Thank you. - user207618
- @Other Very cool. bravo for this answer - L. Vadim
- @ L.Vadim, s-media-cache-ak0.pinimg.com/236x/15/3e/b9/… - user207618
- It's a pity that Explorer doesn't support Array.from - L. Vadim
|
Watching how empty. If it is assumed that they have nothing at all in the markup (including a number of spaces), then you can:
console.log(document.querySelectorAll("td:nth-child(2):empty").length) table, tr, td { border-collapse: collapse; border: 1px solid; } td:empty { background: silver; } <table> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> <tr><td>Строка</td><td>0</td></tr> <tr><td>Строка</td><td></td></tr> </table> - It looks pretty dangerous, any gap or carry breaks the count. But it's still cool :) - user207618
- @Other, yes, it will break. This is written in the answer. - Qwertiy ♦
- Thanks, Cap. Another bad thing is that the scalability is bad. - user207618
- @Other scalability? - Qwertiy ♦
- Well, yes, suddenly you need to change the rules and assume that they are empty and where will be
0? - user207618
|