0

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

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

4

1 回答 1

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>
于 2009-08-17T19:47:13.903 回答