【问题标题】:Converting Nodelist to Array and appending new class to each Node [duplicate]将节点列表转换为数组并将新类附加到每个节点 [重复]
【发布时间】:2017-06-20 09:18:42
【问题描述】:

我查看了大量关于从 NodeList(在本例中为 document.getElementsByClassName())创建数组的文章,以便迭代和修改每个节点上的 className 属性......但是我无法完成任何种改变。看起来有点可笑,但这里是代码:

编辑:问题似乎与从document.getElementsByClassName 创建一个数组有关。它创建的东西看起来像一个数组,但不是真正的数组。请参阅下面的屏幕截图。

var cols = document.getElementsByClassName('cell symmetry');
[].forEach.call(cols, (node) => {
    node.className += ' transiting';
    console.log(node.className);
});

cols 返回一个表格单元格数组。但是使用赋值运算符或尝试将另一个类附加到节点上什么都不做。尝试过document.querySelector、while 循环、for 循环、Array.prototype.slice.call...,但没有骰子。

控制台截图:

【问题讨论】:

  • 似乎在这里工作 jsfiddle.net/qw0fnLhv 。您可以使用更清洁的classList.add 方法
  • 你看过classList吗?类似 classList.add("transiting") developer.mozilla.org/en-US/docs/Web/API/Element/classList
  • 这很奇怪。是的,我试过classList。我看到它在小提琴上工作正常。一定意味着有其他东西阻止它工作。不过谢谢你们,这有帮助。
  • @Jose classList.add() 工作正常,请参阅我的回答。
  • getElementsBy* 方法在每次迭代中收集“实时”,如果 NodeList 实际发生变化,那么它会立即发生变化。如果发生这种情况,那么.length 将在每次迭代中发生变化。你最终会得到一半的.length 和古怪的结果。 querySelectorAll() 返回一个“静态”节点列表。 .length 不会改变。 htmlcssjavascript.com/javascript/…

标签: javascript nodelist


【解决方案1】:

demo 中有详细的注释

演示

/* 
|>.querySelectorAll()
|| Collect all .cell into a "static" NodeList

|>Array.from()
|| Convert NodeList into an array

|~.getElementsByClassName() 
|| Does the same as .querySelectorAll({.class})
|| except that gEBCN() returns a "Live Collection"
|| which means that it changes in real time so
|| that there's a good chance you may end up with
|| half the expected length of the collection.
|| 99% of the time .qSA() is the better choice
|| because it returns a "static" NodeList.
*/
var cellArray = Array.from(document.querySelectorAll('.cell'));

/*
|>.forEach()
|| Iterate through array and invoke a function on 
|| each node.
*/
cellArray.forEach(function(node, idx) {

  /*
  |>.classList.add()
  || Adds a class to a node's list of classes
  */
  node.classList.add('transiting');
});
table {
  width: 50%
}

.cell {
  border: 1px solid blue;
  background:blue;
}

.transiting {
  border: 5px solid red;
}
<table>
  <tr>
    <td class='cell'>&nbsp;</td>
    <td class='cell'>&nbsp;</td>
    <td class='cell'>&nbsp;</td>
  </tr>
  <tr>
    <td class='cell'>&nbsp;</td>
    <td class='cell'>&nbsp;</td>
    <td class='cell'>&nbsp;</td>
  </tr>
  <tr>
    <td class='cell'>&nbsp;</td>
    <td class='cell'>&nbsp;</td>
    <td class='cell'>&nbsp;</td>
  </tr>
  <tr>
    <td class='cell'>&nbsp;</td>
    <td class='cell'>&nbsp;</td>
    <td class='cell'>&nbsp;</td>
  </tr>
</table>

【讨论】:

    【解决方案2】:

    正如您所描述的那样,您的示例似乎没有任何问题。请注意下面我的全部变成蓝色,这意味着正在添加新的d 类。

    var cols = document.getElementsByClassName('cell symmetry');
    [].forEach.call(cols, (node) => {
        node.className += ' transiting';
        console.log(node.className);
    });
    .transiting {
      color: #00F;
    }
     <p class="cell symmetry">A</p>
     <p class="cell symmetry">B</p>
     <p class="cell symmetry">C</p>

    我认为除了你的 sn-p 之外可能还有其他事情发生。您的节点不应有td.cell.symmetry 的className 输出。 className 应该与 cell symmetry transitioning 类似。

    是否还有更多代码,可能是您正在将node.className 设置为某些其他地方的代码?它可能会使其处于一种时髦的状态,然后会弄乱这段代码。

    您也可以在尝试更改之前尝试console.log(node.className)。

    【讨论】:

    • 是的。所以className 我认为出现是因为这是 React 应用程序的一部分。但是,我不明白为什么 classList 也不起作用。我会做更多的挖掘和更新。
    • 啊,好吧。 React 使情况有所不同。你不应该使用document.querySelector 之类的。 React 不喜欢这样。相反,您应该根据您发送的一些属性在组件的渲染函数中设置 className。
    • 对,除了我在某些 DOM 元素上切换 CSS 类。 ReactDOM.findDOMNode 已被弃用,使用 this.refs 在这里不起作用,因为 DOM 节点数组并非全部位于单个组件中。所以这有点复杂。也远离 jQuery。
    • 确实如此,但这就是 React 的陷阱。使用 React 时,你不能真正进行直接的 DOM 操作。你需要从 React 的方式来处理它,否则会发生奇怪的事情。您可能需要以某种方式重构您的代码来解决这个问题。另一种可能的选择是通过使用 CSS 定位某个父类来应用过渡。
    • 为此我们需要大量代码。您可能会在同一代码的第二次循环中看到 50,而第一次是空的(可能是预渲染)。记住 React 第一次启动时,什么都没有。
    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多