在Web组件内动态添加元素

最大888

我想创建一个Web组件,其中包含可以添加到其中的元素的列表。例如,如果我有一个初始模板,例如:

const template = document.createElement("template");
template.innerHTML = `<input type="text"></input><button>add div</button>`;

class MyElement extends HTMLElement {
  constructor() {
    super();
    this._shadowRoot = this.attachShadow({ mode: "open" });
    this._shadowRoot.appendChild(template.content.cloneNode(true));
    const button = this._shadowRoot.querySelector("button");
    button.addEventListener("click", this.addDiv);
  }
  addDiv(e) {
    // ...
  }
}
customElements.define("my-element", MyElement);

每次单击该按钮时,<div>都会添加一个包含来自输​​入字段的文本的,从而创建如下内容:

<input type="text"></input><button>add div</button>
<div>first text from input added</div>
<div>second text from input added</div>
...
超锐利

在您的情况下,您不能insertAjacentHTML()在Shadow DOMshadowRoot属性使用因为Shadow Root无法实现Element接口。

使用bind(this)

更好的解决方案是appendChild(shadowRoot属性使用但是,您需要bind()click事件回调添加特殊操作

在事件回调中,它确实引用了触发事件的元素,而不是在其中定义回调的对象。

为了获得对自定义元素的引用(为了访问Shadow DOM中的输入元素shadowRoot,请调用bind(this)inside addEventListener()

button.addEventListener( "click", this.addDiv.bind( this ) )

请参阅下面的完整示例:

const template = document.createElement("template");
template.innerHTML = `<input type="text"></input><button>add div</button>`;

class MyElement extends HTMLElement {
  constructor() {
    super();
    this._shadowRoot = this.attachShadow({ mode: "open" });
    this._shadowRoot.appendChild(template.content.cloneNode(true));
    const button = this._shadowRoot.querySelector("button");
    button.addEventListener("click", this.addDiv.bind( this ) );
  }
  addDiv(e) {
    var div = document.createElement( 'div' )
    div.textContent = this.shadowRoot.querySelector( 'input' ).value
    this.shadowRoot.appendChild( div )
  }
}
customElements.define("my-element", MyElement);
<my-element></my-element>


使用箭头功能

另一个解决方案是使用箭头功能使用箭头功能,此功能不会重新定义,因此您无需使用bind()

class MyElement extends HTMLElement {
  constructor() {
    super()
    const sh = this.attachShadow( { mode: "open" } )
    sh.innerHTML = `<input type="text"></input><button>add div</button>`
    const button = sh.querySelector( "button" )
    button.onclick = ev => {
      let div = document.createElement( "div" )
      div.textContent = sh.querySelector( "input" ).value
      sh.appendChild( div )
    }
  }
}
customElements.define( "my-element", MyElement )
<my-element></my-element>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JAXB动态添加元素

来自分类Dev

动态添加元素上的jQuery CSS()

来自分类Dev

动态添加元素到布局

来自分类Dev

动态添加元素到XML文件

来自分类Dev

动态在图像顶部添加元素

来自分类Dev

qTip和动态添加元素的问题

来自分类Dev

动态添加元素上的jQuery CSS()

来自分类Dev

使用angularJS动态添加元素

来自分类Dev

动态添加元素到页面顶部

来自分类Dev

HTML:使用JS动态添加元素

来自分类Dev

动态添加元素上的click事件

来自分类Dev

更改动态添加元素的背景

来自分类Dev

向 Angularjs 动态添加元素

来自分类Dev

在HashMap内的HashSet中添加元素

来自分类Dev

在R中的列表内添加元素

来自分类Dev

在关联数组中的元素内添加元素

来自分类Dev

动态添加元素上的jQuery选择器

来自分类Dev

动态地向对象添加元素

来自分类Dev

在Groovy中向ArrayList动态添加元素

来自分类Dev

添加元素动态后的jQuery绑定事件

来自分类Dev

为什么不按角度动态添加元素

来自分类Dev

在C#中向UI动态添加元素

来自分类Dev

如何动态地向Source添加元素?

来自分类Dev

动态添加元素上的jQuery UI droppable吗?

来自分类Dev

MVVM动态向滚动视图添加元素

来自分类Dev

在SwiftUI中向VStack动态添加元素

来自分类Dev

JS事件侦听器,用于动态添加元素

来自分类Dev

添加元素动态后的jQuery绑定事件

来自分类Dev

动态添加元素时,jQuery无法正常工作