我正在遵循有关填字游戏的教程,但是在按键触发selectLetter()函数后,当我在chrome中运行该消息时,会收到以下消息:


  “未捕获的TypeError:无法读取未定义的属性'clueA'” *


当我在firefox开发人员中运行该消息时:


  “ TypeError:currentLetter.dataset未定义”。*


我尝试在console.log中使用typeof来跟踪currentLetter和currentLetter.dataset的值,但这让我更加困惑。



var allLetters;
var currentLetter;
var wordLetters;
var acrossClue;
var downClue;
var typeDirection = "right";
window.onload = init;

function init() {
  allLetters = document.querySelectorAll("table#crossword span");
  currentLetter = allLetters[0];
  var acrossID = currentLetter.dataset.clueA;
  var downID = currentLetter.dataset.clueD;
  acrossClue = document.getElementById(acrossID);
  downClue = document.getElementById(downID);
  formatPuzzle(currentLetter);
  for (var i = 0; i < allLetters.length; i++) {
    allLetters[i].style.cursor = "pointer";
    allLetters[i].onmousedown = function(e) {
      formatPuzzle(e.target);
    }
  }
  document.onkeydown = selectLetter;
  console.log("----init function----\n" + "Current Letter Type: " + typeof currentLetter + " \ncurrentLetter.dataset Type: " + typeof currentLetter.dataset);
}


function formatPuzzle(puzzleLetter) {
  currentLetter = puzzleLetter;
  for (var i = 0; i < allLetters.length; i++) {
    allLetters[i].style.backgroundColor = "";
  }
  acrossClue.style.color = "";
  downClue.style.color = "";

  if (currentLetter.dataset.clueA !== undefined) {
    acrossClue = document.getElementById(currentLetter.dataset.clueA);
    acrossClue.style.color = "blue";
    wordLetters = document.querySelectorAll("[data-clue-a=" + currentLetter.dataset.clueA + "]");
    for (i = 0; i < wordLetters.length; i++) {
      wordLetters[i].style.backgroundColor = "rgb(231,231,255)";
    }
  }
  if (currentLetter.dataset.clueD !== undefined) {
    downClue = document.getElementById(currentLetter.dataset.clueD);
    downClue.style.color = "red";
    wordLetters = document.querySelectorAll("[data-clue-d=" + currentLetter.dataset.clueD + "]");
    for (i = 0; i < wordLetters.length; i++) {
      wordLetters[i].style.backgroundColor = "rgb(255,231,231)";
    }
  }
  if (typeDirection === "right") {
    currentLetter.style.backgroundColor = "rgb(191,191,255)";
  } else {
    currentLetter.style.backgroundColor = "rgb(255,191,191)";
  }
  console.log("----formatPuzzle function----\n" + "Current Letter Type: " + typeof currentLetter + " \ncurrentLetter.dataset Type: " + typeof currentLetter.dataset);
}


function selectLetter(e) {
  var leftLetter = currentLetter.dataset.left;
  var upLetter = currentLetter.dataset.up;
  var rightLetter = currentLetter.dataset.right;
  var downLetter = currentLetter.dataset.down;
  var userKey = e.keyCode;
  console.log("----selectLetter function----\n" + "Current Letter Type: " + typeof currentLetter + " \ncurrentLetter.dataset Type: " + typeof currentLetter.dataset + "\n.dataset.up:" + currentLetter.dataset.up + "\nkeycode:" + userKey);
  if (userKey === 37) {
    formatPuzzle(leftLetter);
  } else if (userKey === 38) {
    formatPuzzle(upLetter);
  } else if (userKey === 39) {
    formatPuzzle(rightLetter);
  } else if ((userKey === 40) || (userKey === 13)) {
    formatPuzzle(downLetter);
  } else if ((userKey === 8) || (userKey === 46)) {
    currentLetter.textContent = "";
  } else if (userKey === 32) {
    switchTypeDirection;
  } else if (65 <= userKey <= 90) {
    currentLetter.textContent = getChar(userKey);
    if (typeDirection === "right") {
      formatPuzzle(rightLetter);
    } else {
      formatPuzzle(downLetter);
    }
  }
  e.preventDefault();
}

function switchTypeDirection() {

}

function getChar(keyNum) {
  return String.fromCharCode(keyNum);
}

<table id="crossword">
  <caption>Today's Crossword</caption>
  <tr>
    <td><span id="c1_1" data-letter="C" data-right="c1_2" data-left="c1_8" data-down="c2_1" data-up="c11_1" data-clue-a="a1" data-clue-d="d1"></span><sup>1</sup></td>
    <td><span id="c1_2" data-letter="A" data-right="c1_3" data-left="c1_1" data-down="c2_2" data-up="c11_2" data-clue-a="a1" data-clue-d="d2"></span><sup>2</sup></td>
    <td><span id="c1_3" data-letter="M" data-right="c1_4" data-left="c1_2" data-down="c2_3" data-up="c11_3" data-clue-a="a1" data-clue-d="d3"></span><sup>3</sup></td>
    <td><span id="c1_4" data-letter="O" data-right="c1_5" data-left="c1_3" data-down="c2_4" data-up="c10_4" data-clue-a="a1" data-clue-d="d4"></span><sup>4</sup></td>
    <td><span id="c1_5" data-letter="M" data-right="c1_6" data-left="c1_4" data-down="c2_5" data-up="c11_5" data-clue-a="a1" data-clue-d="d5"></span><sup>5</sup></td>
    <td><span id="c1_6" data-letter="I" data-right="c1_7" data-left="c1_5" data-down="c2_6" data-up="c11_6" data-clue-a="a1" data-clue-d="d6"></span><sup>6</sup></td>
    <td><span id="c1_7" data-letter="L" data-right="c1_8" data-left="c1_6" data-down="c2_7" data-up="c11_7" data-clue-a="a1" data-clue-d="d7"></span><sup>7</sup></td>
    <td><span id="c1_8" data-letter="E" data-right="c1_1" data-left="c1_7" data-down="c2_8" data-up="c11_8" data-clue-a="a1" data-clue-d="d8"></span><sup>8</sup></td>
    <td class="blank"></td>
  </tr>





我希望箭头键可以更改拼图中箭头键所指向的单元格的背景色rgb(191,191,255),但是代码会停止运行,并且会在控制台上显示类型错误。

最佳答案

该错误意味着为currentLetter分配了一个不是DOM元素的值(因为每个DOM元素都有一个dataset属性)。

您分配给currentLetter的唯一位置是在init中:

currentLetter = allLetters[0];


formatPuzzle

currentLetter = puzzleLetter;


allLetters[0]将是一个DOM元素。

puzzleLetterformatPuzzle的参数,因此让我们看一下该函数是否已调用并传递了不是DOM元素的内容。

selectLetter中,您使用不同的参数多次调用formatPuzzle,但是它们都不是DOM元素。例如。 leftLettercurrentLetter.dataset.left的值。

也许你想做

var leftLetter = documnent.getElementById(currentLetter.dataset.left);
// etc






确保仅将DOM元素传递给函数,并尽可能避免使用全局变量。

关于javascript - 如何查找和修复未定义属性的类型错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55383959/

10-10 10:14