简体   繁体   中英

For loop if statement recognizes == but not < in Javascript

I'm trying to create a script that will replace certain values in my table with other values. I generated a table in HTML that works fine. I want to replace the elements in the 4th column in each of the first 3 rows with the value 1/8".

var rows = document.getElementsByTagName('tr');

for (var i = 0; i < rows.length; i++) {
 var cells = rows[i].getElementsByTagName('td');
 if (i < 3) {
  cells[3].innerHTML = '1/8"';
 }
}

Using the above code does nothing for my table, however using the following code replaces the 4th element with my value

var rows = document.getElementsByTagName('tr');

for (var i = 0; i < rows.length; i++) {
 var cells = rows[i].getElementsByTagName('td');
 if (i == 3) {
 cells[3].innerHTML = '1/8"';
 }
}

Why does == work, but < does not? Are there easy ways to print out the outputs of the for loop so i can debug easier - Im new to coding. Thanks!

Edit: Here is the HTML I'm using, along with the CSS (which is probably not necessary)

HTML5:

<div class="eo_product_listing_table">
<table class="tableizer-table">
<thead>
  <tr class="tableizer-firstrow">

    <th>Item #</th>
    <th>Type</th>
    <th>Operting Temp.</th>
    <th>O.D.</th>
    <th>Length</th>
    <th>Material</th>
    <th>Junction</th>
    <th>Price</th>
    <th>Order</th>
  </tr>
</thead>

<tbody>
  <!--1/8-->
  <!--6-->
  <tr>
    <td>NB4-CAXL-14U-12-AAG</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>3/8"</td>
    <td>6"</td>
    <td>316SS</td>
    <td>Grounded</td>
    <td>$51.00</td>
    <td><a href="/">Order</a></td>
  </tr>
  <tr>
    <td>NB4-CAXL-14U-12-AAU</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>3/8"</td>
    <td>6"</td>
    <td>316SS</td>
    <td>Ungrounded</td>
    <td>$53.00</td>
    <td><a href="/">Order</a></td>
  </tr>
  <tr>
    <td>NB4-CAXL-14U-12-AAE</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>3/8"</td>
    <td>6"</td>
    <td>316SS</td>
    <td>Exposed</td>
    <td>$51.00</td>
    <td><a href="/">Order</a></td>
  </tr>
  <!--12-->
  <tr>
    <td>NB4-CAXL-14U-12-ABG</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>3/8"</td>
    <td>12"</td>
    <td>316SS</td>
    <td>Grounded</td>
    <td>$53.00</td>
    <td><a href="/">Order</a></td>
  </tr>
  <tr>
    <td>NB4-CAXL-14U-12-ABU</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>3/8"</td>
    <td>12"</td>
    <td>316SS</td>
    <td>Ungrounded</td>
    <td>$55.00</td>
    <td><a href="/">Order</a></td>
  </tr>
  <tr>
    <td>NB4-CAXL-14U-12-ABE</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>3/8"</td>
    <td>12"</td>
    <td>316SS</td>
    <td>Exposed</td>
    <td>$53.00</td>
    <td><a href="/">Order</a></td>
  </tr>
  <!--18-->
  <tr>
    <td>NB4-CAXL-14U-12-ACG</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>1/8"</td>
    <td>18"</td>
    <td>316SS</td>
    <td>Grounded</td>
    <td>$57.00</td>
    <td><a href="/">Order</a></td>
  </tr>
  <tr>
    <td>NB4-CAXL-14U-12-ACU</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>1/8"</td>
    <td>18"</td>
    <td>316SS</td>
    <td>Ungrounded</td>
    <td>$59.00</td>
    <td><a href="/">Order</a></td>
  </tr>
  <tr>
    <td>NB4-CAXL-14U-12-ACE</td>
    <td>K</td>
    <td>-270°C to 1372°C</br>–454°F to 2501°F</td>
    <td>1/8"</td>
    <td>18"</td>
    <td>316SS</td>
    <td>Exposed</td>
    <td>$57.00</td>
    <td><a href="/">Order</a></td>
  </tr>
</tbody>

CSS3

.eo_product_listing_table {}

.tableizer-table tr:nth-child(even) {
   background-color: #eee;
}

table.tableizer-table {
  font-size: 12px;
  border: 1px solid #CCC;
  font-family: Arial, Helvetica, sans-serif;
  color: black;
}

.tableizer-table td {
  padding: 4px;
  margin: 3px;
  border: 0px solid #CCC;
  text-align: left;
  color: black;
}

.tableizer-table th {
  background-color: #104E8B;
  color: #FFF;
  font-weight: bold;
  text-align: left;
}

.tableizer-firstrow {
  background-color: #000;
  color: whtie;
}

For the first three iterations of your loop ( i < 3 ) you are just setting the same cell's innerHTML to '1/8"' again and again and again.

Maybe you'd meant to do:

var rows = document.getElementsByTagName('tr');

for (var i = 0; i < rows.length; i++) {
  var cells = rows[i].getElementsByTagName('td');
  if (i < 3) {
    cells[i].innerHTML = '1/8"';
  }
}

You use your script for ALL <tr> tag's, just like @Luaan said - look into your HTML file. That's mean, you must count form 1, not form 0. Then everything works fine.

var rows = document.getElementsByTagName('tr');

for (var i = 1; i < rows.length; i++) {
 var cells = rows[i].getElementsByTagName('td');
 if (i < 4) {
 cells[3].innerHTML = '1/8"';
 }
}

Okay, now that you've posted your HTML, the error is in plain sight :) The first tr you find contains no td s whatsoever, so you get an unhandled error and the function breaks.

You need to only look for rows in the tbody , excluding any that might be in theader or tfooter . It's also generally a good idea to make only iterate through rows of a particular table, rather than all rows in the whole document. You could even give the specific tbody an id and access it directly, avoiding all those issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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