2009-08-17 39 views
0

我遇到了奇怪的故障。看起来,如果我在一个锚元素上同时拥有一个ID和NAME属性,那么document.getElementById会失败。如果我删除了NAME,它就会起作用。我在Firefox 3.5(最新版)中看到了这一点,但尚未检查其他浏览器。如果锚也具有名称属性,则Javascript - getElementById失败

这是一个错误还是故意的?

+2

无法重现。几乎可以肯定你在同一时间做的其他改变。 – 2009-08-17 19:42:22

+0

也许在被移除的名称标签和另一个名称标签之间存在冲突? – Zed 2009-08-17 19:43:19

+0

我唯一见过类似的东西是在IE中 - 它不区分名称/ ID属性,所以getElementById可以返回一些事情,如果发生碰撞,您并不总是期待这样的事情。 – Pat 2009-08-17 19:51:02

回答

4

我从来没有听说过这样的错误,所以我试图重现它并失败。这表明你误诊了这个问题,或者至少没有提供足够的信息。

我使用Firefox 3.5和以下代码进行了测试。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd"> 
<title>Test of getElementById with named anchors</title> 
<h1>Test of getElementById with named anchors</h1> 

<h2><a name="one" id="one">First section</a></h2> 
<p>The quick brown fox</p> 

<h2><a name="two" id="second">Second section</a></h2> 
<p>The quick brown fox</p> 

<script type="text/javascript">  
if (document.getElementById('one')) { 
     document.write("<p>First section found - id matches name<\/p>"); 
} 

if (document.getElementById('second')) { 
     document.write("<p>Second section found - id does not match name<\/p>"); 
} 
</script> 
+0

相同,我无法在我的3.5.2 firefox上重现它。 :\ – codingbear 2009-08-17 19:49:03

+0

感谢您的测试人员。我不知道目前是什么造成的,但如果我在未来有一段时间,我会调查。我想知道它是否以我的代码,或者我无法完全描述的奇怪边缘情况。 – Geuis 2009-08-17 20:05:58

相关问题