最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to check in JavaScript if table row with specific index exist? - Stack Overflow

matteradmin7PV0评论

I have this JavaScript block code:

var trows = table.getElementsByTagName('tbody')[0].rows;  
for(var j = ((pageNumber - 1) * rowsPerPage); j < ((pageNumber) * rowsPerPage); j++)
{
  trows[j].style.display = 'table-row';
}

Before I implement this row:

 trows[j].style.display = 'table-row';

I need to check if table row with scpecific index exists.

How to check in JavaScript if table row with specific index exist?

I have this JavaScript block code:

var trows = table.getElementsByTagName('tbody')[0].rows;  
for(var j = ((pageNumber - 1) * rowsPerPage); j < ((pageNumber) * rowsPerPage); j++)
{
  trows[j].style.display = 'table-row';
}

Before I implement this row:

 trows[j].style.display = 'table-row';

I need to check if table row with scpecific index exists.

How to check in JavaScript if table row with specific index exist?

Share Improve this question edited Nov 11, 2014 at 15:37 Kashif Umair Liaqat 1,0741 gold badge19 silver badges27 bronze badges asked Nov 11, 2014 at 15:18 MichaelMichael 13.6k59 gold badges170 silver badges315 bronze badges 3
  • Is jQuery an option? – puelo Commented Nov 11, 2014 at 15:20
  • 2 trows[j].value != undefined – Mahmoud.Eskandari Commented Nov 11, 2014 at 15:22
  • No I don't use jQuery – Michael Commented Nov 11, 2014 at 15:27
Add a ment  | 

2 Answers 2

Reset to default 5

Since undefined is falsey, you can just do this:

if (trows[j]) {
    trows[j].style.display = 'table-row';
}
if(typeof trows[j] !== 'undefined') {
    trows[j].style.display = 'table-row';
}

You can check if the type is undefined.

Post a comment

comment list (0)

  1. No comments so far