JavaScript 错误,后遗漏名称。操作员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16107074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:17:10  来源:igfitidea点击:

JavaScript Error, Missing Name After . Operator

javascript

提问by 0xhughes

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...

我正在尝试获取隐藏跨度的innerHTML。JavaScript 来自 iframe HTML 页面,隐藏的跨度驻留在父页面中。从父级访问列表的内容时,不同的功能起作用,但我似乎无法达到我的跨度......

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.

上面的代码行驻留在 for 循环中,当它遍历 i 时,它将从每个单独的跨度中获取数据。隐藏跨度从 ID“span1”开始,通过多达 10-40k 个不同的隐藏跨度。

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!

无论如何,我有一种感觉,它必须通过尝试连接字符串 int i 来做一些事情。我假设我是一个 int 反正。有什么想法吗?非常感谢大家!

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...

编辑 - Words,并将 innerHTML 部分添加到不起作用的代码行中。不确定这是否会有所作为...

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!

Edit2 - 大家都很好回答,学到了一些很好的句法技巧:) 我只是移动了父级。mplungjan 的评论和 Jacob T. Nielson 的回答所推荐的代码前面的部分。出于某种原因,我仍然按照建议使用括号时出现错误,但我肯定会将括号塞进我的记忆中,以备将来类似的情况使用!

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

:)

:)

采纳答案by Jacob T. Nielsen

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

我认为您要做的是在父页面上获取第 i 个 span 元素。正确的?

You can do it like this

你可以这样做

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

回答by Daniel A. White

Try changing it to an indexer.

尝试将其更改为索引器。

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

回答by Trolleymusic

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

如果parent括号中的 是一个对象并且您试图访问类似的内容,parent.span1那么您需要使用括号表示法而不是点。

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

document.getElementById(parent["span"+i]);应该工作正常。