在某些情况下,getElementsByName在FireFox上不起作用。 [英] getElementsByName doesn't work on FireFox in some cases.
问题描述
< html>
< head>
< script>
/ * AddChild * /
function ac()
{
var c;
var p = document.getElementById(" p");
for(var i = 0; i< 5; i ++){
c = document.createElement(" DIV"); //创建''div''元素。
c.id =" ch";
c.name =" ch";
c.innerHTML =" child" + i;
p.insertBefore(c,null); //添加''div''元素作为孩子。
}
}
/ * CountChildren * /
函数cc()
{
//子项数是名称为
''的元素数ch''
var c = document.getElementsByName(''ch'');
// IE6报告5,但Firefox报告为什么?
// getElementsByName在FireFox上不起作用?
alert(c.length);
}
< / script>
< / head>
< body onload =" ac();">
< div id =" p"> parents:< / div>
< input type =" button" value ="有多少孩子''父母''拥有?"
onclick =" cc();"
< / body>
< / html>
<html>
<head>
<script>
/* AddChild */
function ac()
{
var c;
var p = document.getElementById("p");
for (var i = 0; i < 5; i++) {
c = document.createElement("DIV"); // Create ''div'' element.
c.id = "ch";
c.name = "ch";
c.innerHTML = "child " + i;
p.insertBefore(c, null); // Add ''div'' element as child.
}
}
/* CountChildren */
function cc()
{
// The number of children is the number of elements whose name is
''ch''
var c = document.getElementsByName(''ch'');
// IE6 reports 5, but Firefox reports 0. why?
// getElementsByName doesn''t work on FireFox?
alert(c.length);
}
</script>
</head>
<body onload="ac();">
<div id="p">parents:</div>
<input type="button" value="How many children ''parents'' has?"
onclick="cc();">
</body>
</html>
推荐答案
briggs写道:
请仅发布纯文本。
[...]
briggs wrote:
Please post plain text only.
[...]
c = document.createElement(" DIV"); //创建''div''元素。
c.id =" ch";
c.name =" ch";
c.innerHTML =" child" + i;
p.insertBefore(c,null); //将''div''元素添加为子元素。
[...]
var c = document.getElementsByName(''ch'');
// IE6报告5,但Firefox报告为什么?
// getElementsByName在FireFox上不起作用?
alert(c.length);
c = document.createElement("DIV"); // Create ''div'' element.
c.id = "ch";
c.name = "ch";
c.innerHTML = "child " + i;
p.insertBefore(c, null); // Add ''div'' element as child.
[...]
var c = document.getElementsByName(''ch'');
// IE6 reports 5, but Firefox reports 0. why?
// getElementsByName doesn''t work on FireFox?
alert(c.length);
''name''不是div元素的有效属性。如果你检查
元素,它们有一个''name''属性但是它不被
getElementsByName识别。
插入可以有名称的元素(比如INPUT)并且它有效。
[...]
-
Rob
''name'' is not a valid attribute for div elements. If you check the
elements, they have a ''name'' attribute but it is not recognised by
getElementsByName.
Insert elements that can have a name (say INPUTs) and it works.
[...]
--
Rob
谢谢,Rob
''name''不是div元素的有效属性。
但是,以下代码在Firefox中运行良好。
< html>
< head>
< script>
/ * CountChildren * /
函数cc()
{
//数字儿童是名字的元素数量
''ch''
var c = document.getElementsByName(''ch'');
// IE和Firefox报告5.
alert(c.length);
}
< / script>
< / head>
< body>
< div id =" p"> ;父母:
< div name =" ch"> child 0< / div>
< div name =" ch"> child 1< / div>
< div name =" ch"> child 2< / div>
< div name =" ch"> child 3< / div>
< div name =" ch"> child 4< / div>
< input type =" button" value ="有多少孩子''父母''拥有?"
onclick =" cc();"
< / body>
< / html>
我认为...
如果以上代码有效,我的第一篇文章中的代码应该是也工作了。
Thanks, Rob
''name'' is not a valid attribute for div elements.
But, the following code works well in Firefox.
<html>
<head>
<script>
/* CountChildren */
function cc()
{
// The number of children is the number of elements whose name is
''ch''
var c = document.getElementsByName(''ch'');
// Both IE and Firefox reports 5.
alert(c.length);
}
</script>
</head>
<body>
<div id="p">parents:
<div name="ch">child 0</div>
<div name="ch">child 1</div>
<div name="ch">child 2</div>
<div name="ch">child 3</div>
<div name="ch">child 4</div>
<input type="button" value="How many children ''parents'' has?"
onclick="cc();">
</body>
</html>
I think...
if the above code works, the codes in my first post should work too.
RobG写道:
RobG wrote:
briggs写道:
请发布纯文本。
他/她做了:
|内容类型:text / plain; charset =" euc-kr"
[...]
briggs wrote:
Please post plain text only.
He/she did:
| Content-Type: text/plain; charset="euc-kr"
[...]
c = document.createElement(" DIV"); //创建''div''元素。
c.id =" ch";
c.name =" ch";
c.innerHTML =" child" + i;
p.insertBefore(c,null); //将''div''元素添加为子元素。
[...]
c = document.createElement("DIV"); // Create ''div'' element.
c.id = "ch";
c.name = "ch";
c.innerHTML = "child " + i;
p.insertBefore(c, null); // Add ''div'' element as child.
[...]
var c = document.getElementsByName(''ch'');
// IE6报告5,但Firefox报告为什么?
// getElementsByName对FireFox不起作用?
alert(c.length);
var c = document.getElementsByName(''ch'');
// IE6 reports 5, but Firefox reports 0. why?
// getElementsByName doesn''t work on FireFox?
alert(c.length);
''name''不是div元素的有效属性。
''name'' is not a valid attribute for div elements.
True。
如果检查元素,它们有一个''name''属性
错误。创建的元素对象添加了一个名称属性。
c.getAttribute(" name")// null
但它无法识别通过getElementsByName。
这是什么原因。
插入可以有名字的元素(比如INPUT)并且它有效。
True.
If you check the elements, they have a ''name'' attribute
False. The element object created is added a `name'' property.
c.getAttribute("name") // null
but it is not recognised by getElementsByName.
Which is the reason of this.
Insert elements that can have a name (say INPUTs) and it works.
True。
PointedEars
True.
PointedEars
这篇关于在某些情况下,getElementsByName在FireFox上不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!