使用 `querySelector` 选择节点时遇到问题,使用数据属性值。

huangapple go评论63阅读模式
英文:

Trouble with selecting node with querySelector using data attribute values

问题

我在我的HTML中有9个类似以下的div:

    <div class="game-cell" data-row="1" data-column="1"></div>
    <div class="game-cell" data-row="1" data-column="2"></div>
    <div class="game-cell" data-row="1" data-column="3"></div>
    <div class="game-cell" data-row="2" data-column="1"></div>
    <div class="game-cell" data-row="2" data-column="2"></div>
    <div class="game-cell" data-row="2" data-column="3"></div>
    <div class="game-cell" data-row="3" data-column="1"></div>
    <div class="game-cell" data-row="3" data-column="2"></div>
    <div class="game-cell" data-row="3" data-column="3"></div>

我试图使用以下代码选择其中一个:

document.querySelector(`.game-cell, [data-row="1"], [data-column="2"]`)

但是在打印输出时,无论我指定什么值,被选中的节点始终是具有行和列值为1的节点:

<div class="game-cell" data-row="1" data-column="1">

为什么会发生这种情况?我做错了什么?我应该如何选择我想要的节点?

英文:

I have 9 divs in my html like so:

    &lt;div class=&quot;game-cell&quot; data-row=&quot;1&quot; data-column=&quot;1&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;game-cell&quot; data-row=&quot;1&quot; data-column=&quot;2&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;game-cell&quot; data-row=&quot;1&quot; data-column=&quot;3&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;game-cell&quot; data-row=&quot;2&quot; data-column=&quot;1&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;game-cell&quot; data-row=&quot;2&quot; data-column=&quot;2&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;game-cell&quot; data-row=&quot;2&quot; data-column=&quot;3&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;game-cell&quot; data-row=&quot;3&quot; data-column=&quot;1&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;game-cell&quot; data-row=&quot;3&quot; data-column=&quot;2&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;game-cell&quot; data-row=&quot;3&quot; data-column=&quot;3&quot;&gt;&lt;/div&gt;

I am trying to select one of them at a time using

document.querySelector(`.game-cell, [data-row=&quot;1&quot;], [data-column=&quot;2&quot;]`)

but on printing the output the node that get selected is the one with row and column values 1, no matter what values i specify:

&lt;div class=&quot;game-cell&quot; data-row=&quot;1&quot; data-column=&quot;1&quot;&gt;

Why is this happening? Am I doing something wrong? How can I select the node I want?

答案1

得分: 2

移除列:

document.querySelector('.game-cell[data-row="1"][data-column="2"]');

替代方法:

let myelement;
document.querySelectorAll('.game-cell').forEach(cell => {
  myrow = cell.dataset.row;
  mycolumn = cell.dataset.column;
  if (myrow == x && mycolumn == y) {
    myelement = cell;
  }
});
英文:

Remove the columns:

document.querySelector(&#39;.game-cell[data-row=&quot;1&quot;][data-column=&quot;2&quot;]&#39;);

An alternative:

let myelement;
document.querySelectorAll(&#39;.game-cell&#39;).forEach(cell =&gt; () { 
  myrow = cell.dataset.row;
  mycolumn = cell.dataset.column;
  if (myrow == x &amp;&amp; mycolumn == y) { //you specify &#39;x&#39; and &#39;y&#39;
    myelement = cell;
  }
});

答案2

得分: 1

你也可以使用选择器来匹配所有带有'game-cell'类的元素,然后可以使用纯JS基于它们的行和列属性来过滤结果:

const cells = document.querySelectorAll('.game-cell');
const targetCell = Array.from(cells).find(cell => 
  cell.getAttribute('data-row') === '1' && cell.getAttribute('data-column') === '2'
);
英文:

You can also use a selector to match all elements with the 'game-cell' class so that you can then filter the results based on their row and column attributes using pure js :

const cells = document.querySelectorAll(&#39;.game-cell&#39;);
const targetCell = Array.from(cells).find(cell =&gt; 
  cell.getAttribute(&#39;data-row&#39;) === &#39;1&#39; &amp;&amp; cell.getAttribute(&#39;data-column&#39;) === &#39;2&#39;
);

huangapple
  • 本文由 发表于 2023年2月27日 16:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定