简体   繁体   English

“document.querySelector(...) 为空”,即使它存在

[英]"document.querySelector(...) is null" even tho it exist

I created 64 class in numbers (1,2,3,4...) and it seems that there are a lot of case specific things to do so I can use them normally like this我创建了 64 class 的数字(1,2,3,4 ...),似乎有很多特定于案例的事情要做所以我可以像这样正常使用它们

document.querySelector(".\\" + number)

For some reason when I try to create an img inside the div it says that it dosen't exist.出于某种原因,当我尝试在 div 中创建一个 img 时,它说它不存在。

Here's the code related to the error:这是与错误相关的代码:

HTML: HTML:

<div id="board">
<div style="width: 61.2219px; height: 61.2219px;" class="1"></div>
<div style="width: 61.2219px; height: 61.2219px; background-color: white;" class="2"></div>
<div style="width: 61.2219px; height: 61.2219px;" class="3"></div>
<div style="width: 61.2219px; height: 61.2219px; background-color: white;" class="4"></div>
<div style="width: 61.2219px; height: 61.2219px;" class="5"></div>
<div style="width: 61.2219px; height: 61.2219px; background-color: white;" class="6"></div>
...
<script src="main.js"></script>

JS:记者:

while (x != 8)
{ **// Generates DIV**
  if (last == 0)
  {
    last = 1;
  }
  else
  {
    last = 0;
  }

  while (y != 8)
  {
    var div = document.createElement("div");
    div.style.width = width;
      div.style.height = width;
    if (last == 0)
    {
      div.style.backgroundColor = "white";
      last = 1;
    } else 
    {
      last = 0;
    }
    div.classList.add(((x*8)+y)+1)
    board.appendChild(div);
    y++;
  }
  y=0
  x++;
}
...
while(x != startpos_len)
{
  console.log(x);
  let name = ".\\" + (x+1)
  var imgPiece = document.createElement("img");
  switch(startpos[x])
  {
    case "BR":
      img.src = "."
    default:
      // alert("Startpos contain errors")
  }
  **document.querySelector("div" + name).appendChild(imgPiece);**
  x++;
}

PS: This is a chess game PS:这是棋类游戏

I tried reading documentation on Mozilla, w3school and similar error on stack overflow, but nothing was working.我尝试阅读有关 Mozilla、w3school 和堆栈溢出的类似错误的文档,但没有任何效果。

Also defer in didn't change anything也推迟并没有改变任何东西

Changing to ID's or adding letters in the class didn't work更改为 ID 或在 class 中添加字母无效

Your selector syntax is incorrect.您的选择器语法不正确。

Your options are...你的选择是...

Preferably use id attributes that are actually selectable.最好使用实际上可选择的id属性。 This makes sense given you seem to want unique elements (chessboard positions)鉴于您似乎想要独特的元素(棋盘位置),这是有道理的

<div style="width: 61.2219px; height: 61.2219px;" id="pos-1"></div>
document.getElementById(`pos-${x+1}`)?.appendChild(imgPiece);

OR要么

Not recommended... keep the (bad) numeric classes and use an attribute selector.不推荐...保留(坏的)数字类并使用属性选择器。 Note the quotes around the value are required注意值周围的引号是必需的

document.querySelector(`div[class='${x+1}']`)?.appendChild(imgPiece);

Note that this only works with class attributes that are single-token.请注意,这仅适用于单标记的 class 属性。 If there are other classes involved, eg class="foo bar baz 1" , it gets more complex如果涉及其他类,例如class="foo bar baz 1" ,它会变得更复杂

document.querySelector(
  `div[class='1'], div[class^='1 '], div[class$=' 1'], div[class*=' 1 ']`
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM