2016-09-19 2 views
0

Ich habe eine Tabelle, die automatisch von einer externen Software (die ich verwenden muss und keine Ahnung wie es funktioniert) generiert wird.Ändern von Text in DOM-Tabelle

Ich versuche, jedes Element in der Tabelle mit datafld == "n0" von etwas wie diesem zu ändern: 15HS01 15F24:HS632401.COUT zu nur 15F24.

Ich versuche, diesen Code zu verwenden:

// Get all the tables in the document. 
var doc_tables = document.all.tags("TABLE"); 
for (ii = 0; ii < doc_tables.length; ii++) 
{ 
// Process each table. 
FireZone(doc_tables(ii).id) 
} 


// Access the data table object (use the dataFld string) 
function FireZone(tableID) 
{ 
// Get the 'specified' table object. 
var table = eval("document.all." + tableID); 

// Skip the header rows and get to the data rows in table 
var nHeadRows = table.rows.length - table.all.tags("TBODY").length; 

// Iterate all rows in table.... 
if(table.rows.length > 0) 
{ 
    table 
    for (i=nHeadRows; i < table.rows.length; i++) 
    { 
     table(i).width = 1200; 
     // Iterate all cells in each row - 

     for (j=0; j < table.rows(i).cells.length; j++) 
     { 

      // Locate cells by "dataFld" - interested in "n0" binding 
      // Get the Cell value 
      for (k=0; k < table.rows(i).cells(j).children.length; k++) 
      { 
       if(table.rows(i).cells(j).children[k].dataFld == "n0") 
       { 
        var value = table.rows(i).cells(j).children[k].innerText; 
        if (value.search("15F") != -1) 
        { 
         table.rows(i).cells(j).children[k].innerText = value.substr(value.search("15F",5)); 
        } 
       } 
      } 
     } 
    } 
} 
} 

Aber aus irgendeinem Grund dieser Code nichts zu tun scheint, noch es jedoch einen Fehler wirft.

Der HTML-Block zu jeder Zeile entsprechenden sieht wie folgt aus:

<TBODY> 

<TR> 

<TD style="FONT-FAMILY: Georgia, serif; COLOR: white" noWrap><SPAN dataFld=t0>9/16/2016 11:33:51 AM</SPAN></TD> 

<TD style="FONT-FAMILY: Georgia, serif; COLOR: white"><SPAN dataFld=n0>15HS01 15F24:HS632401.COUT</SPAN></TD> 

<TD style="FONT-FAMILY: Georgia, serif; COLOR: white" noWrap><SPAN dataFld=v0>0</SPAN></TD></TR></TBODY> 

Das Skript auf jeden Fall laufen zu sein, so wäre jede Hilfe dankbar.

Antwort

0

Hope this zu beginnen hilft, fand ich einige Javascript-Fehler auf Ihrem Code

// Get all the tables in the document. 
 
var doc_tables = document.querySelectorAll("TABLE"); 
 

 
for (ii = 0; ii < doc_tables.length; ii++) 
 
{ 
 
    console.log(doc_tables[ii].getAttribute("id")) 
 
// Process each table. 
 
FireZone(doc_tables[ii]) 
 

 
} 
 

 

 
// Access the data table object (use the dataFld string) 
 
function FireZone(table) 
 
{ 
 
// Get the 'specified' table object. 
 
var table = eval(table); 
 

 
var tds = table.querySelectorAll("td"); 
 
// Iterate all rows in table.... 
 
if(tds.length > 0) 
 
{ 
 
    for (i=0; i < tds.length; i++) 
 
    { 
 
        var value = tds[i].innerText; 
 
        
 
        if (value.indexOf("15F") != -1) 
 
        { 
 
         var newValue = value.substr(value.indexOf("15F"),5); 
 
         console.log(newValue); 
 
         tds[i].innerText = value.substr(value.indexOf("15F",5)); 
 
        } 
 
    } 
 
} 
 
}
td {color:#000}
<table border="1"> 
 
<TBODY> 
 

 
<TR> 
 

 
<TD style="FONT-FAMILY: Georgia, serif; COLOR: white" noWrap><SPAN dataFld=t0>9/16/2016 11:33:51 AM</SPAN></TD> 
 

 
<TD style="FONT-FAMILY: Georgia, serif; COLOR: white"><SPAN dataFld=n0>15HS01 15F24:HS632401.COUT</SPAN></TD> 
 

 
<TD style="FONT-FAMILY: Georgia, serif; COLOR: white" noWrap><SPAN dataFld=v0>0</SPAN></TD></TR></TBODY> 
 
    </table>

+0

Vielen Dank für Ihren Kommentar, ich erhalte eine Fehlermeldung, Tabelle nicht Methode enthält ' querySelectorAll' –