<tfoot id='T7F4F'></tfoot>
  1. <i id='T7F4F'><tr id='T7F4F'><dt id='T7F4F'><q id='T7F4F'><span id='T7F4F'><b id='T7F4F'><form id='T7F4F'><ins id='T7F4F'></ins><ul id='T7F4F'></ul><sub id='T7F4F'></sub></form><legend id='T7F4F'></legend><bdo id='T7F4F'><pre id='T7F4F'><center id='T7F4F'></center></pre></bdo></b><th id='T7F4F'></th></span></q></dt></tr></i><div id='T7F4F'><tfoot id='T7F4F'></tfoot><dl id='T7F4F'><fieldset id='T7F4F'></fieldset></dl></div>
    • <bdo id='T7F4F'></bdo><ul id='T7F4F'></ul>

    <legend id='T7F4F'><style id='T7F4F'><dir id='T7F4F'><q id='T7F4F'></q></dir></style></legend>

      <small id='T7F4F'></small><noframes id='T7F4F'>

      Vanilla Web 组件自定义事件属性和属性

      时间:2023-09-04

          <tfoot id='nQReo'></tfoot>
        1. <legend id='nQReo'><style id='nQReo'><dir id='nQReo'><q id='nQReo'></q></dir></style></legend>
        2. <i id='nQReo'><tr id='nQReo'><dt id='nQReo'><q id='nQReo'><span id='nQReo'><b id='nQReo'><form id='nQReo'><ins id='nQReo'></ins><ul id='nQReo'></ul><sub id='nQReo'></sub></form><legend id='nQReo'></legend><bdo id='nQReo'><pre id='nQReo'><center id='nQReo'></center></pre></bdo></b><th id='nQReo'></th></span></q></dt></tr></i><div id='nQReo'><tfoot id='nQReo'></tfoot><dl id='nQReo'><fieldset id='nQReo'></fieldset></dl></div>

          <small id='nQReo'></small><noframes id='nQReo'>

            <bdo id='nQReo'></bdo><ul id='nQReo'></ul>
                <tbody id='nQReo'></tbody>

                本文介绍了Vanilla Web 组件自定义事件属性和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在没有任何框架的情况下使用 Web 组件,实现自定义事件的正确方法是什么?例如,假设我有一个自定义元素 x-pop-out 有一个自定义事件 pop 我希望以下所有操作都能正常工作:

                Using Web Components without any framework, what is the proper way to implement a custom event? For example, say I have a custom element x-pop-out that has a custom event of pop I would want all of the following to work:

                <x-pop-out onpop="someGlobal.doSomething()"/>

                var el = document.getElementsByTagName('x-pop-out')[0];
                el.onpop = ()=> someGlobal.doSomething();
                //or
                el.addEventListener('pop', ()=> someGlobal.doSomething());
                

                最后一个我知道该怎么做,但是我需要自定义实现属性和每个 getter/setter 吗?另外,eval() 是从属性执行字符串的适当方式吗?

                The last one I get how to do, but do I need to custom implement the attribute and a getter / setter for each? Also, is eval() the appropriate way to execute the string from the attribute?

                推荐答案

                事件监听器 解决方案(第三种)是最简单的,因为您不必定义任何特殊的东西来捕获事件.

                The event listener solution (the third one) is the easiest because you don't have to define anything special to catch the event.

                事件处理程序解决方案需要制作一个eval()(第一个,来自属性)或显式调用函数(第二个).

                The event handler solutions need to make an eval() (first one, from attribute) or to call the fonction explicitely (second one).

                如果你不能使用 eval 你可以改为解析属性字符串.

                If you can't use eval you can instead parse the attribute string.

                customElements.define( 'x-pop-out', class extends HTMLElement {
                    connectedCallback() {
                        this.innerHTML = `<button id="Btn">pop</button>`
                
                        this.querySelector( 'button' ).onclick = () => {
                            this.dispatchEvent( new CustomEvent( 'pop' ) )
                            if ( this.onpop )
                                this.onpop()
                            else
                                eval( this.getAttribute( 'onpop' ) )
                        }            
                    }
                } )
                
                XPO.addEventListener( 'pop', () => console.info( 'pop' ) )

                <x-pop-out id=XPO onpop="console.log( 'onpop attribute' )"></x-pop-out>
                <hr>
                <button onclick="XPO.onpop = () => console.log( 'onpop override' )">redefine onpop</button>

                这篇关于Vanilla Web 组件自定义事件属性和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:不将其他绑定函数覆盖到 window.onresize 的最佳做法 下一篇:javascript 将事件处理程序绑定到水平滚动

                相关文章

                最新文章

                  <bdo id='xU2Yz'></bdo><ul id='xU2Yz'></ul>
              • <tfoot id='xU2Yz'></tfoot>

                    <small id='xU2Yz'></small><noframes id='xU2Yz'>

                  1. <i id='xU2Yz'><tr id='xU2Yz'><dt id='xU2Yz'><q id='xU2Yz'><span id='xU2Yz'><b id='xU2Yz'><form id='xU2Yz'><ins id='xU2Yz'></ins><ul id='xU2Yz'></ul><sub id='xU2Yz'></sub></form><legend id='xU2Yz'></legend><bdo id='xU2Yz'><pre id='xU2Yz'><center id='xU2Yz'></center></pre></bdo></b><th id='xU2Yz'></th></span></q></dt></tr></i><div id='xU2Yz'><tfoot id='xU2Yz'></tfoot><dl id='xU2Yz'><fieldset id='xU2Yz'></fieldset></dl></div>

                    <legend id='xU2Yz'><style id='xU2Yz'><dir id='xU2Yz'><q id='xU2Yz'></q></dir></style></legend>