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

在DOM中按id获取元素

  •  0
  • lte__  · 技术社区  · 7 年前

    我在创造一个 element

    console.log('about to create modal');
    this.createModal(
      'There are unsubmitted changes in this Access Request.',
      'Do you wish to discard them?',
      'Yes',
      'No',
      'tabCloseModal'
    );
    console.log('created modal');
    const modal = this.shadowRoot.querySelector('#tabCloseModal');
    console.log(`modal = ${modal}`);
    modal.addEventListener('px-modal-accepted', function(e) {
      console.log('removing tab');
      this.removeTab(index);
    });
    

    createModal 创建元素:

      createModal(headerText, bodyText, acceptText, rejectText, id, opened = true) {
        const modal = document.createElement('px-modal');
        //assign parameters
        document.swQuerySelector('body').appendChild(modal);
        console.log('Child appended');
        modal.visible = true;
        this.fire('modal-visible');
      }
    

    不管我做什么(我累了) swQuerySelector swQuerySelectorAll , querySelector , querySelectorAll ),我好像找不到模态。当我记录它的时候,它只是显示为空的,未定义的或者[object]或者类似的东西,我从来没有去“删除tab”。我错过了什么?模态正在显示,但接受的事件侦听器的映射不起作用。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Steven Spungin    7 年前

    要解决此问题:

    1. 从返回模态 createModal ,并确认它在调用代码中按预期工作。

    2. 如果是这样,问题就出在你自己身上 this.shadowRoot.querySelector querySelector command line 直到你得到你的模态。尝试在开发工具中找到模态 elements 打开窗户看看它在哪里。如果没有看到DOM的布局,我们就无法给出父元素的确切路径。

    阴影区域有时会变得“阴暗”。。。