Firefox的getElementById问题 [英] getElementById problem with Firefox

查看:118
本文介绍了Firefox的getElementById问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:

我使用特定数量的< div id =" MyTest">填充页面容器在另一个< div>内容器。


for(i = 0; i< MyString.length; i ++)

document.write(''< div id =" MyTest"

style =" position:absolute; top:0px; left:0; height:12; width:12; text-align:center">''+ MyString [i] + ''< / div>'');


现在如果MyString包含6个字符,我最终得到6< div id =" MyTest">容器。


使用IE,我可以按如下方式访问这些容器:


for(i = 0; i< MyString.length ; i ++){

var Obj = MyTest [i] .style;

Obj.top = NewPositionTop;

Obj.left = NewPositionLeft + i;

}


现在使用Firefox这不起作用

Javascript错误:file:/// c: /test/mytest.js,209行:MyTest [i]没有属性


和Javascript控制台建议:

警告:ID /引用的元素全球范围内的NAME。请使用W3C标准document.getElementById()。


所以我决定使用getElementById(),但一直收到错误。

我试过:

Obj1 = document.getElementById(" MyTest");

然后访问Obj1 [i] .style.top / left但


Javascript错误:file:/// c:/test/mytest.js,第210行:Obj1 [i]没有属性


我也试过Obj = document.getElementById(& ; MyTest")[i] .style;


,结果相同。


是什么让我无法访问特定条目在getElementById(" MyTest")列表/数组中?


有人能指出我正确的方向吗?


谢谢,Robi

I have the following problem:
I populate a page with a specific amount of <div id="MyTest"> containers inside another <div> container.

for (i=0; i < MyString.length; i++)
document.write(''<div id="MyTest"
style="position:absolute;top:0px;left:0;height:12; width:12;text-align:center">''+MyString[i]+''</div>'');

Now if MyString contains 6 characters, I end up with 6 <div id="MyTest"> containers.

With IE I am able to access these containers as follows:

for (i=0; i < MyString.length; i++) {
var Obj=MyTest[i].style;
Obj.top=NewPositionTop;
Obj.left=NewPositionLeft+i;
}

Now with Firefox this didn''t work
Javascript Error: file:///c:/test/mytest.js, line 209: MyTest[i] has no properties

and the Javascript Console suggests:
Warning: Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.

so I decided to use getElementById(), but keep getting errors.
I tried:
Obj1=document.getElementById("MyTest");
and then access Obj1[i].style.top/left but

Javascript Error: file:///c:/test/mytest.js, line 210: Obj1[i] has no properties

I also tried Obj=document.getElementById("MyTest")[i].style;

with the same result.

What is it that keeps me from accessing a specific entry in the getElementById("MyTest") list/array?

can anyone point me in the right direction?

Thanks, Robi

推荐答案

2005年7月13日15:00,Robi写道:
On 13/07/2005 15:00, Robi wrote:
I有以下问题:
我使用特定数量的< div id =" MyTest">填充页面。容器在另一个< div>内容器。


你做不到。在整个文档中,id属性值必须是唯一的。

for(i = 0; i< MyString.length; i ++)
document.write (''< div id =" MyTest" [...]


当你在编写每个元素时,你可以追加i的值来生成

唯一值。


[snip]

使用IE我可以访问这些容器[使用同名的全局变量]


是的,你可以。但是,在我看来,这是一个可怕的功能,而且你不应该使用它。

不应该使用它。


[snip]

Obj1 = document.getElementById(" MyTest");
然后访问Obj1 [i] .style.top / left但是
I have the following problem:
I populate a page with a specific amount of <div id="MyTest"> containers inside another <div> container.
You cannot do that. An id attribute value must be unique throughout an
entire document.
for (i=0; i < MyString.length; i++)
document.write(''<div id="MyTest" [...]
As you''re writing each element, you can append the value of i to produce
unique values.

[snip]
With IE I am able to access these containers [using a global variable with the same name]
Yes, you can. However, this is a terrible feature in my opinion, and you
shouldn''t use it.

[snip]
Obj1=document.getElementById("MyTest");
and then access Obj1[i].style.top/left but




getElementById方法返回一个元素,而不是一个集合,

因为只有一个元素应该有一个匹配的值。如果

你有多个潜在的匹配,最常见的结果是

first(来源)将继续返回。


继续上述建议,您可以将代码更改为:


var obj = document.getElementById( ''MyTest''+ i);


if(obj&& obj.style){

/ * ... * /

}


[snip]


Mike


-

迈克尔·温特

前缀主题与[新闻]在回复e-之前邮件。



The getElementById method returns a single element, not a collection,
because there should only ever be one element with a matching value. If
you do have multiple potential matchs, the most common result is that
first (in source order) will always be returned.

Continuing the suggestion above, you can alter your code to:

var obj = document.getElementById(''MyTest'' + i);

if(obj && obj.style) {
/* ... */
}

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


Robi写道:
我有以下问题:
我用特定数量的< div填充页面ID = QUOT; MyTest的">容器在另一个< div>内(i = 0; i< MyString.length; i ++)
document.write(''< div id =" MyTest"
style = " position:absolute; top:0px; left:0; height:12; width:12; text-align:center">''+ MyString [i] +''< / div>'');


不允许有超过1个具有相同ID的元素!


请尝试以下内容:


myHtml ='''';

for(i = 0; i< MyString.length; i ++)

myHtml + =''< div id =" MyTest''+ i +''"''+

''style =" position:absolute; top:0px; left:0; height:12; width:12 ; text-align:center">''+

MyString [i] +''< / div>'');

document.write(myHtml) ;


最好有一个separte css不要复制相同的东西x次

使用IE我可以按如下方式访问这些容器:

for(i = 0; i< MyString.length; i ++){
var Obj = MyTest [i] .style;
Obj.top = NewPositionTop;
Obj.left = NewPositionLeft + i;
}
I have the following problem:
I populate a page with a specific amount of <div id="MyTest"> containers inside another <div> container.

for (i=0; i < MyString.length; i++)
document.write(''<div id="MyTest"
style="position:absolute;top:0px;left:0;height:12; width:12;text-align:center">''+MyString[i]+''</div>'');
it is not allowed to have more than 1 element with same ID !

so try something as :

myHtml = '''';
for (i=0; i < MyString.length; i++)
myHtml += ''<div id="MyTest''+i+''"''+
''style="position:absolute;top:0px;left:0;height:12 ;width:12;text-align:center">''+
MyString[i]+''</div>'');
document.write(myHtml);

Would be better to have a separte css to do not copy same thing x times
With IE I am able to access these containers as follows:

for (i=0; i < MyString.length; i++) {
var Obj=MyTest[i].style;
Obj.top=NewPositionTop;
Obj.left=NewPositionLeft+i;
}




是的,这是IE的工作方式


尝试:

for(i = 0; i< MyString.length; i ++){

var Obj = document.all?

MyTest [i] .style:

document.getElementById?

document.getElementById(''MyTest''+ i).style:

document.layers [''MyTest''+ i];

Obj.top = NewPositionTop +''px'';

Obj.left = NewPositionLeft +' 'px'';

}


请注意,document.write()

不是最新的脚本的方式

请参阅:createElement和appendChild

另请参阅:cloneNode(询问谷歌)


-

Stephane Moriaux et son [moins] vieux Mac



yes that''s IE way of work

try :
for (i=0; i < MyString.length; i++) {
var Obj = document.all?
MyTest[i].style :
document.getElementById?
document.getElementById(''MyTest''+i).style :
document.layers[''MyTest''+i] ;
Obj.top = NewPositionTop+''px'';
Obj.left = NewPositionLeft+''px'';
}

Notice that document.write()
is not an "up to date" way to script
See : createElement and appendChild
See also : cloneNode (ask to google)

--
Stephane Moriaux et son [moins] vieux Mac


Michael Winter写道:
Michael Winter wrote:
On 13/07/2005 15:00 ,Robi写道:
On 13/07/2005 15:00, Robi wrote:
我有以下问题:
我使用特定数量的< div id =" MyTest">填充页面。容器
在另一个< div>内容器。
I have the following problem:
I populate a page with a specific amount of <div id="MyTest"> containers
inside another <div> container.



你做不到。 id属性值在整个文档中必须是唯一的。



You cannot do that. An id attribute value must be unique throughout an
entire document.

for(i = 0; i< MyString.length; i ++)
document.write (''< div id =" MyTest" [...]
for (i=0; i < MyString.length; i++)
document.write(''<div id="MyTest" [...]



在编写每个元素时,可以追加i的值来生成
独特的价值。

[snip]



As you''re writing each element, you can append the value of i to produce
unique values.

[snip]

使用IE我可以访问这些容器[使用同名的全局变量]
With IE I am able to access these containers [using a global variable with the same name]



是的,你可以。但是,在我看来,这是一个可怕的功能,你不应该使用它。



Yes, you can. However, this is a terrible feature in my opinion, and you
shouldn''t use it.




谢谢,从现在开始将避免/忽略它的使用。



Thanks, will avoid/omit the use of it from now on.

Obj1 = document.getElementById(" MyTest");
然后访问Obj1 [i] .style.top / left但
Obj1=document.getElementById("MyTest");
and then access Obj1[i].style.top/left but



getElementById方法返回单个元素,而不是集合,
因为应该只有前夕r是具有匹配值的一个元素。如果
你确实有多个潜在的匹配,最常见的结果是
首先(按来源顺序)将永远返回。

继续上面的建议,你可以改变你的代码:

var obj = document.getElementById(''MyTest''+ i);

if(obj&& obj.style){
/ * ... * /
}



The getElementById method returns a single element, not a collection,
because there should only ever be one element with a matching value. If
you do have multiple potential matchs, the most common result is that
first (in source order) will always be returned.

Continuing the suggestion above, you can alter your code to:

var obj = document.getElementById(''MyTest'' + i);

if(obj && obj.style) {
/* ... */
}




好​​吧,我做了忘记在我的帖子中提到,那是我使用的/ workaround /



脚本是由其他人编写的,我试图让它适应工作

至少在我的浏览器中也是如此。不知何故,我确实知道元素ID应该在文档中是唯一的,但是当我看到这段代码时,我认为这是一种很好的方式来访问对象。


感谢您的回复,并且如果我已经完成了这件事,请让我不相信

正确与否。


我不明白你为什么要检查(obj&& obj.style)。

不会检查(obj.style)是否足够?此外,既然我知道我设定了风格,那么(obj)就够了吗?


Robi



well, I did "forget" to mention in my post, that that was the /workaround/
I used.
The script was written by someone else and I was trying to adapt it to work
at least in my browser as well. Somehow I did know that element IDs should
be unique in a document although when I saw this code I thought it was a
neat way to access the objects.

Thanks for your reply and for getting me out of my doubt if I had done it
correctly or not.

What I don''t understand is why do you check for (obj && obj.style).
Wouldn''t a check for just (obj.style) suffice? Besides, since I know I set
the style, wouldn''t (obj) be enough?

Robi


这篇关于Firefox的getElementById问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆