最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Error, Missing Name After . Operator - Stack Overflow

programmeradmin2浏览0评论

I am trying to get the innerHTML of a hidden span. The JavaScript is from an iframe HTML page, and the hidden span resides in the parent page. A different function works when accessing contents of a list from the parent, but I can't seem to get at my span...

WORKS

document.getElementById(parent.genL[i]);

DOESNT WORK

document.getElementById(parent."span"+i).innerHTML;
- SyntaxError: missing name after . operator

The above line of code resides in a for loop and as it iterates through i it will grab data from each separate span. the hidden spans start at ID "span1" through upwards of 10-40k different hidden spans.

Anyways, I have a feeling that it has to do something with trying to concatenate the string int i. I assume i is an int anyways. Any thoughts? Thanks so much everyone!

Edit - Words, and added the innerHTML portion to the doesn't work line of code. Not sure if that will be making a difference or not...

Edit2 - Great answers everyone, learned some good syntactical tricks :) I simply moved the parent. portion to the front of the code as reccomend by the comment of mplungjan and the answer from Jacob T. Nielson. For some reason I still got the error using the brackets as suggested, but I will definitely tuck the brackets into my memory for future similar situations!

parent.document.getElementById("span"+i).innerHTML;

:)

I am trying to get the innerHTML of a hidden span. The JavaScript is from an iframe HTML page, and the hidden span resides in the parent page. A different function works when accessing contents of a list from the parent, but I can't seem to get at my span...

WORKS

document.getElementById(parent.genL[i]);

DOESNT WORK

document.getElementById(parent."span"+i).innerHTML;
- SyntaxError: missing name after . operator

The above line of code resides in a for loop and as it iterates through i it will grab data from each separate span. the hidden spans start at ID "span1" through upwards of 10-40k different hidden spans.

Anyways, I have a feeling that it has to do something with trying to concatenate the string int i. I assume i is an int anyways. Any thoughts? Thanks so much everyone!

Edit - Words, and added the innerHTML portion to the doesn't work line of code. Not sure if that will be making a difference or not...

Edit2 - Great answers everyone, learned some good syntactical tricks :) I simply moved the parent. portion to the front of the code as reccomend by the comment of mplungjan and the answer from Jacob T. Nielson. For some reason I still got the error using the brackets as suggested, but I will definitely tuck the brackets into my memory for future similar situations!

parent.document.getElementById("span"+i).innerHTML;

:)

Share Improve this question edited Apr 19, 2013 at 14:54 0xhughes asked Apr 19, 2013 at 14:32 0xhughes0xhughes 2,7534 gold badges27 silver badges38 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

Try changing it to an indexer.

document.getElementById(parent["span"+i]);

If the parent in the brackets is an object and you're trying to access something like parent.span1 then you need to use bracket notation instead of the dot.

document.getElementById(parent["span"+i]); should work fine.

I think what you are trying to do is get the i-th span element on the parent page. Correct?

You can do it like this

var s = parent.document.getElementsByTagName('span')[i];
s.innerHTML // <-- access innerHTML
发布评论

评论列表(0)

  1. 暂无评论