************************************* HTML代码************* ***********************]
<table id="myTable">
<tr>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">A</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">B</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">C</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">D</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">E</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">F</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">G</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">H</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">I</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">J</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">K</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">L</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">M</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">N</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">O</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">P</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">Q</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">R</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">S</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">T</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">U</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">V</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">W</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">X</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">Y</a></td>
<td><a id="LettersInTheTable" onclick="LetterPressed()" href="">Z</a></td>
</tr>
************************** Javascript代码******************** ***************
var Words=["GREEN","LOVE","HOPE","JOYOUS"];
var Letter="";
var z;
var x;
function LetterPressed(){
x = document.getElementById("LettersInTheTable").text;
document.getElementById("Letterzz").innerHTML = x;
window.alert(x);
}
我不确定您要完成什么,但是如果您只是想跟踪在html页面上按下了什么键,则无法使用]]
document.addEventListener('keydown', event => {});
我认为您可以做这样的事情
document.addEventListener('keydown', event => { let key = event.key.toLowerCase() console.log(key) document.getElementById('Letterzz').innerHTML = key }
目前,您的HTML没有一个名为Letterzz的ID,所以我不确定您试图将按下的键放在哪里。希望这会有所帮助。
您在每个html元素中都使用了相同的ID。如果您想访问它们,那么理想情况下,它们应该是不同的。
是因为document.getElementById("LettersInTheTable").text;
,此代码在DOM中搜索带有Id
的元素,并返回找到的第一个元素,并且内部文本为A
,所以请进行以下更改