最新消息: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)

javascript - Object doesn't support property or method 'getElementsById' in IE11 - Stack Overflow

matteradmin7PV0评论

I am trying to use java-script to export html data into excel. The funny thing is that it DOES work when I use getElementsByTagName instead of getElementById. However, I need to pinpoint id elements and thus 'getElementById' is what I need (I guess). When I debug the below code in IE it gives me:

Object doesn't support property or method 'getElementsById'

Here is what I've got:

HTML (as an idea only):

<body>
<table>
<tr>
  <td>content 1</td>
  <td>content 2</td>
      <td id="R">content I need</td>
      <td>some other content</td>
   </tr>
</table>
</body>

and acpanying JS

<script type="text/javascript">
function write_to_excel() 
{
str="";

var mytable = document.getElementById("R")[0];
var row_Count = mytable.rows.length;
var col_Count = mytable.getElementById("R")[0].getElementById("R").length;    

var ExcelApp = new ActiveXObject("Excel.Application");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelSheet.Application.Visible = true;

for(var i=0; i < row_Count ; i++) 
{   
    for(var j=0; j < col_Count; j++) 
    {           
        str= mytable.getElementById("R")[i].getElementById("R")[j].innerHTML;
        ExcelSheet.ActiveSheet.Cells(i+1,j+1).Value = str;
    }
}
}
</script>

I have the feeling - it's trifle but ... Thanks in advance!)

I am trying to use java-script to export html data into excel. The funny thing is that it DOES work when I use getElementsByTagName instead of getElementById. However, I need to pinpoint id elements and thus 'getElementById' is what I need (I guess). When I debug the below code in IE it gives me:

Object doesn't support property or method 'getElementsById'

Here is what I've got:

HTML (as an idea only):

<body>
<table>
<tr>
  <td>content 1</td>
  <td>content 2</td>
      <td id="R">content I need</td>
      <td>some other content</td>
   </tr>
</table>
</body>

and acpanying JS

<script type="text/javascript">
function write_to_excel() 
{
str="";

var mytable = document.getElementById("R")[0];
var row_Count = mytable.rows.length;
var col_Count = mytable.getElementById("R")[0].getElementById("R").length;    

var ExcelApp = new ActiveXObject("Excel.Application");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelSheet.Application.Visible = true;

for(var i=0; i < row_Count ; i++) 
{   
    for(var j=0; j < col_Count; j++) 
    {           
        str= mytable.getElementById("R")[i].getElementById("R")[j].innerHTML;
        ExcelSheet.ActiveSheet.Cells(i+1,j+1).Value = str;
    }
}
}
</script>

I have the feeling - it's trifle but ... Thanks in advance!)

Share Improve this question edited Feb 21, 2014 at 19:04 AlexShevyakov asked Feb 21, 2014 at 18:53 AlexShevyakovAlexShevyakov 4237 silver badges21 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

The getElementById method returns a single DOM element (if you have more than one HTML element with the same ID then your page is buggy but browsers won't plain because 10 years ago it was a mon bug that lots of people make). As such the statement:

document.getElementById("R")[0]

Makes no sense whatsoever. Instead, what you want is:

var myTD = document.getElementById("R");

If you have a page structure like this:

<table id='T'>
<tr>
  <td>content 1</td>
  <td>content 2</td>
  <td>content I need</td>
  <td>some other content</td>
</tr>
</table>

And want to iterate each column in each row, you'd do it like this:

var mytable = document.getElementById("T");
var table_rows = mytable.getElementsByTagName('tr');
for (var row=0;row<table_rows.length;row++) {
    var row_columns = table_rows[row].getElementsByTagName('td');
    for (var col=0;col<row_columns.length;col++) {
        var item = row_columns[col];
        // process item here
    }
}

See the documentation of HTMLElement for more info on how to navigate the DOM: https://developer.mozilla/en/docs/Web/API/Element

Full documentation of the DOM API: https://developer.mozilla/en/docs/DOM

You may also check out the relevant docs on MSDN instead of MDN for IE specific stuff but I prefer MDN because it documents the patibility level (what other browsers implement a feature) of the API.

IDs must be unique.

Therefore, the function you're looking for is called getElementById (singular)

Post a comment

comment list (0)

  1. No comments so far