javascript 未捕获的类型错误:无法在 Google Chrome 中设置未定义的属性“值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21094572/
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
Uncaught TypeError: Cannot set property 'value' of undefined in Google Chrome
提问by Opec
In my page A, there is a textarea defined like this:
在我的页面 A 中,有一个 textarea 定义如下:
<textarea readonly wrap="Physical" cols="50" rows="2" name="Attr">
The page A opens page B in a popup (by window.open('PageB')
).
页面 A 在弹出窗口中打开页面 B (by window.open('PageB')
)。
In the page B, the button "Save" executes this line of code:
在页面B中,按钮“保存”执行这行代码:
window.parent.opener.document.all.Attr.value='Value 1, Value 2,';
This one causes an error in Google Chrome (error: "Uncaught TypeError: Cannot set property 'value' of undefined") but works perfectly with Internet Explorer and Mozilla Firefox. I have read that document.all
is old and should be replaced by document.getElementById
which I did but with no success (error: "Uncaught TypeError: Cannot set property 'value' of null").
这会导致 Google Chrome 出现错误(错误:“Uncaught TypeError: Cannot set property 'value' of undefined”)但与 Internet Explorer 和 Mozilla Firefox 完美配合。我读过它document.all
是旧的,应该替换为document.getElementById
我所做的但没有成功(错误:“未捕获的类型错误:无法设置空值的属性”)。
If I replace document.all
by document.forms[0]
, it works in all browsers including Google Chrome. But that doesn't seem to be a good solution as the page B could be called by other pages than Page A and that could have more than one form.
如果我替换document.all
为document.forms[0]
,则它适用于包括 Google Chrome 在内的所有浏览器。但这似乎不是一个好的解决方案,因为页面 B 可以被页面 A 之外的其他页面调用,并且可能有多个表单。
Could anyone tell me what I am doing wrong?
谁能告诉我我做错了什么?
Thanks in advance!
提前致谢!
Opec.
欧佩克。
回答by scrblnrd3
Why can't you just assign it an id an access it through that?
为什么你不能给它分配一个 id 并通过它访问它?
<textarea id="text" readonly wrap="Physical" cols="50" rows="2" name="Attr">
and in your javascript
并在您的 javascript 中
document.getElementById("text").value="Value 1, value 2"
Here is a JSFiddle demonstrating it
这是一个展示它的 JSFiddle