无论我按哪个链接,都按“ x”变量总是保存值“ A”

问题描述 投票:-1回答:3

************************************* 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);
}
javascript variables id
3个回答
0
投票

我不确定您要完成什么,但是如果您只是想跟踪在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,所以我不确定您试图将按下的键放在哪里。希望这会有所帮助。


0
投票

您在每个html元素中都使用了相同的ID。如果您想访问它们,那么理想情况下,它们应该是不同的。


0
投票

是因为document.getElementById("LettersInTheTable").text;,此代码在DOM中搜索带有Id的元素,并返回找到的第一个元素,并且内部文本为A,所以请进行以下更改

© www.soinside.com 2019 - 2024. All rights reserved.