在 IE6 中访问 JavaScript 对象属性的方法

发布于 2024-08-26 03:03:23 字数 1419 浏览 5 评论 0原文

我有一个带有一些属性的 JavaScript 对象。可以说:

var haystack = {
  foo: {value: "fooooo"},
  bar: {value: "baaaaa"}
};

现在,我想访问其中一个属性,但我不知道是哪一个。幸运的是,这个变量确实:

var needle = "foo";

在现代浏览器中,我似乎能够执行以下操作并且它有效:

haystack[needle].value;  # returns "fooooo"

但在 IE6 中,它会抛出一个不稳定的问题,haystack[...] is null or not an object

有没有办法实现我在 IE6 中想要实现的目标?如果是这样,怎么会这样?


编辑 - 添加更多信息以响应下面的评论...


我想要实现的目标实际上与 CKEditor 有关。我编写了一个在 iframe 中打开的插件图像管理器。

然后我想要实现的是将所选图像放回到 CKEditor 的正确实例中(某些页面上可能有多个实例)。

我所做的(我知道这是一个丑陋的黑客),当 iframe 打开时,我在它旁边放置了一个隐藏字段,其中包含实例的名称。所以父页面包含一些像这样的标记:

<iframe><!-- Image manager --></iframe>
<input type="hidden" id="ckinstance" value="article_body" />

那么,当选择插入图像时,在 iframe 内我有一些如下所示的 JavaScript:

var CKEDITOR = window.parent.CKEDITOR;
var instance = window.parent.$('#ckinstance').val();
var img = '<img src="/whatevers/been/selected" />';
CKEDITOR.instances[instance].insertHtml(img);
window.parent.$.modal.close();

这在 FF、Chrome 等中工作正常。只是 IE6 抱怨:

CKEDITOR.instances[...] is null or not an object.

编辑2

我刚刚进行了一些调试,实际上 IE6 似乎在 window.parent.$('#ckinstance').val() 上失败并返回未定义。

所以我所描述的原始问题根本不是问题。

但仍然需要帮助:)

I have a JavaScript object with some properties. Lets say:

var haystack = {
  foo: {value: "fooooo"},
  bar: {value: "baaaaa"}
};

Now, I want to access one of those properties, but I don't know which one. Luckily, this variable does:

var needle = "foo";

In modern browsers I seem to be able to do the following and it works:

haystack[needle].value;  # returns "fooooo"

But in IE6 it throws a wobbly, haystack[...] is null or not an object.

Is there a way to achieve what I'm trying to achieve in IE6? If so, how so?


EDIT - Adding further information in response to the comments below...


What I am trying to achieve is actually related to CKEditor. I haven written a plugin image manager that opens in an iframe.

What I then want to achieve is to place the chosen image back in the correct instance of CKEditor (and there can be more than one instance on some pages).

What I have done (and I know this is an ugly hack), when the iframe is opened I have put a hidden field next to it with the name of the instance. So the parent page contains some markup like this:

<iframe><!-- Image manager --></iframe>
<input type="hidden" id="ckinstance" value="article_body" />

So then, inside the iframe when an image is selected to be inserted I have some JavaScript that looks like this:

var CKEDITOR = window.parent.CKEDITOR;
var instance = window.parent.$('#ckinstance').val();
var img = '<img src="/whatevers/been/selected" />';
CKEDITOR.instances[instance].insertHtml(img);
window.parent.$.modal.close();

This works fine in FF, Chrome, etc. Just IE6 is complaining with:

CKEDITOR.instances[...] is null or not an object.

EDIT 2

I've just done some debugging and actually it looks like IE6 is failing on window.parent.$('#ckinstance').val() and is returning undefined.

So the original problem that I've described is not the problem at all.

Still need help though :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当你花了几个小时为某件事绞尽脑汁,却发现解决方案是:

Tools > Internet Options > Delete Files 

It's quite annoying when you spend a couple of hours scratching your head over something, only to realise the solutions is:

Tools > Internet Options > Delete Files 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文