无法获取childNodes computedStyle

时间:2017-09-17 03:55:40

标签: javascript ecmascript-6

无法获取childNodes computedStyle,收到错误:

  

未捕获的TypeError:   无法在'Window'上执行'getComputedStyle':参数1不是   类型'元素'

let wrappers = document.querySelectorAll('[data-attribute="wrapper"]');
  wrappers = Array.prototype.slice.call(wrappers);

  wrappers.forEach((el) => {
    let wrappersChildren = wrappers.slice.call(el.childNodes, 0);

    wrappersChildren.forEach((el) => {
      const childrenWidth = window.getComputedStyle(el).width;
      ...

我错在哪里?

1 个答案:

答案 0 :(得分:0)

let wrappers = document.querySelectorAll('[data-attribute="wrapper"]');
  wrappers = Array.prototype.slice.call(wrappers);

  wrappers.forEach((el) => {
    //Using el.childNodes is the mistake... returns text nodes too.
    //change to el.children
    let wrappersChildren = wrappers.slice.call(el.children, 0);

    wrappersChildren.forEach((el) => {
      const childrenWidth = window.getComputedStyle(el).width;
      ...