访问命名元素 [英] Accessing named elements
问题描述
什么时候命名元素用脚本可以访问脚本?
< html>< head>< script type =" text / javascript">
函数ready(){
alert(document.getElementsByName(" div")。length);
}
< / script>< / head>
< body onload =" ready()">
< script type =" text / javascript" ;>
document.open();
document.writeln("< div name = \" div \">< \\ \\ / div>");
document.close();
< / script>
< / body>< / html>
IOW,为什么在调用< body>
元素'的onload处理程序时有0个名为div的元素?我何时可以从文档层次结构中检索此命名的
div?
此练习是必要的,因为我使用DOM编写的代码
(有效)必须在神奇的浏览器上运行,例如IE 5.1 for
Mac,它对DOM的支持充其量只是参差不齐。
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
When are named elements written with script accessible to script?
<html><head><script type="text/javascript">
function ready() {
alert( document.getElementsByName("div").length );
}
</script></head>
<body onload="ready()">
<script type="text/javascript">
document.open();
document.writeln( "<div name=\"div\"><\/div>" );
document.close();
</script>
</body></html>
IOW, why are there 0 elements named div at the time the <body>
element''s onload handler is invoked? When can I retrieve this named
div from the document hierarchy?
This exercise is necessary since code I have written using the DOM
(that works) has to run on God-forsaken browsers such as IE 5.1 for
Mac whose support for the DOM is spotty at best.
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
推荐答案
Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
什么时候命名的元素用脚本可以访问脚本?
< html>< head>< script type =" text / javascript">
function ready(){
alert(document.getElementsByName(" div") ;)。长度);
}
< / script>< / head>
< body onload =" ready()">
< script type =" text / javascript">
document.open();
document.writeln("< div name = \" div \">< \ / div>");
document.close();
< / script>
< / body>< / html>
IOW,为什么在调用< body>
元素'的onload处理程序时有0个名为div的元素?我何时可以从文档层次结构中检索此命名的
div?
When are named elements written with script accessible to script?
<html><head><script type="text/javascript">
function ready() {
alert( document.getElementsByName("div").length );
}
</script></head>
<body onload="ready()">
<script type="text/javascript">
document.open();
document.writeln( "<div name=\"div\"><\/div>" );
document.close();
</script>
</body></html>
IOW, why are there 0 elements named div at the time the <body>
element''s onload handler is invoked? When can I retrieve this named
div from the document hierarchy?
因为getElementsByName()适用于name属性,而不是
标记名称。我想你的意思是打电话给getElementsByTagName()。
[...]
-
Rob
Because "getElementsByName()" works on the name attribute, not the
tag name. I think you mean to call "getElementsByTagName()".
[...]
--
Rob
RobG< rg *** @ iinet.net.auau>这样说:
RobG <rg***@iinet.net.auau> spoke thus:
document.writeln("< div name = \" div \">< \ / div>");
document.writeln( "<div name=\"div\"><\/div>" );
因为" getElementsByName()"适用于name属性,而不是
标记名称。我认为你的意思是调用getElementsByTagName()。
Because "getElementsByName()" works on the name attribute, not the
tag name. I think you mean to call "getElementsByTagName()".
如你所见,元素名为div,所以我希望
getElementsByName()来检索它;在IE 6中,至少,它没有。
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
As you can see, the element is named "div", so I would expect
getElementsByName() to retrieve it; in IE 6, at least, it does not.
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
2005年3月29日01:39,Christopher Benson-Manica写道:
On 29/03/2005 01:39, Christopher Benson-Manica wrote:
何时被命名为元素用脚本可访问的脚本编写?
我相信它会有所不同。使用close方法关闭文档流
可能会刷新输出并更新文档树,但是用户
代理可能会等到它完成执行
SCRIPT块。我现在应该睡着了,所以我的任何测试都要等到今天晚些时候。 :P
[snip]
document.writeln("< div name = \" div \">< \\ \\ / div>");
[snip]
我何时可以从文档层次结构中检索此命名div?
When are named elements written with script accessible to script?
I believe it varies. Closing the document stream with the close method
might flush the output and update the document tree, however a user
agent might wait until the it finishes executing the code in the
SCRIPT block. I should be asleep now, so any tests on my part will
have to wait until later today. :P
[snip]
document.writeln( "<div name=\"div\"><\/div>" );
[snip]
When can I retrieve this named div from the document hierarchy?
你不应该,至少不是名字。 DIV元素
没有名字属性。为什么你没有使用id属性?
[snip]
Mike
-
Michael Winter
替换.invalid与.uk通过电子邮件回复。
You shouldn''t ever be able to, at least not by name. DIV elements
don''t have name attributes. Why aren''t you using the id attribute?
[snip]
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
这篇关于访问命名元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!