代码之家  ›  专栏  ›  技术社区  ›  Prasheel

IE上的HierarchyRequestError 4和DOM操作上的Edge

  •  0
  • Prasheel  · 技术社区  · 6 年前

    我正在使用StencilJS开发一个Web组件。我使用一个槽来呈现HTML,但是我需要在呈现之前修改HTML,为此,我使用queryselector和appendchild函数来操作DOM。这在Chrome上工作得很好,但在IE和EDGE上引发层次结构错误。这是我的代码:

    TSX中的呈现函数:

        render () {
          return (
           <div class={`column-${this.column}`}>
             <slot/>
           </div>
         )
       }
    

    操作DOM的代码:

    componentDidLoad () {
        const container = this.element.shadowRoot.querySelector(`.column-${this.column}`) ?
          this.element.shadowRoot.querySelector(`.column-${this.column}`) : this.element.querySelector(`.column-${this.column}`)
        Array.from(this.element.children).forEach(node => {
          const elem = document.createElement('div')
          elem.appendChild(node)
          container.appendChild(elem)
        })
      }
    

    上面的代码在Chrome上运行得很好,但是在IE的行中抛出了一个错误。

    container.appendChild(elem)
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Prasheel    6 年前

    最后,我找到了这个。

    发生错误的原因是运行时生成的层次结构不正确。

    我只是通过移动解决了这个问题 <slot/> 在外面 <div>

    推荐文章