简体   繁体   English

如何基于addEventListener上的点击事件通过父元素在shadow dom中select元素?

[英]How can I select elements in shadow dom through the parent element based on the click event on addEventListener?

I have a web component, like this:我有一个 web 组件,如下所示:

File: download-bar.js (web component)文件:download-bar.js(网络组件)

class DownloadBar extends HTMLElement {

    constructor() {
        super();
        this.shadowDOM = this.attachShadow({mode: 'open'});        
    }

    connectedCallback() {
        this.render();
    }

    render() {
        this.shadowDOM.innerHTML = `
            <div class="favicon">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="black" width="18px" height="18px"><path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg>
                <span>Download File</span>
            </div>
            <div class="dropdown">
                <a href="" id="xlsx">XLSX</a>
                <a href="" id="pdf">PDF</a>
                <a href="" id="jpeg">JPEG</a>
            </div>
        `;
    }
}

customElements.define('download-bar', DownloadBar);

and I do a selection of elements on the parent shadow element DOM and listen to the click event, like this:我在父阴影元素 DOM 上选择元素并监听 click 事件,如下所示:

File: download-bar.js文件:下载-bar.js

import '../component/download-bar.js';

const downloadElement = document.querySelector('download-bar');

downloadElement.addEventListener('click', (e) => {

    // I would like this:
    if (e.target.classList.contains('favicon')) {
        console.log('True')
    }
})

how can I get child elements from custom elements using shadow dom, if an event from "(e)" that is obtained has a certain class or id, for example class = "favicon"?如果从“(e)”获得的事件具有特定的 class 或 id,例如 class =“favicon”,我如何使用 shadow dom 从自定义元素中获取子元素?

You need to use event.composedPath()您需要使用event.composedPath()

If you use Custom Events be sure to set the composed:true option so your event breaks out of shadowDom如果您使用自定义事件,请务必设置compose composed:true选项,以便您的事件脱离shadowDom

BTW顺便提一句

You can replace all this:您可以替换所有这些:

    constructor() {
        super();
        this.shadowDOM = this.attachShadow({mode: 'open'});        
    }

    connectedCallback() {
        this.render();
    }

    render() {
        this.shadowDOM.innerHTML = ...

with:和:

    connectedCallback() {
        this.attachShadow({mode: 'open'}).innerHTML = ...
    }

You aren't doing anything special in the constructor ;您在constructor中没有做任何特别的事情; so don't define it and let the element run the constructor from HTMLElement.所以不要定义它,让元素从 HTMLElement 运行constructor

attachShadow() both sets and returns this.shadowRoot attachShadow()设置并返回this.shadowRoot

Only when you move/append/drag around Custom Elements in the DOM will the connectedCallback run again, and you might want to split creating shadowDOM to its own constructor只有当您在 DOM 中移动/附加/拖动自定义元素时, connectedCallback才会再次运行,并且您可能希望将创建 shadowDOM 拆分为它自己的constructor


Also see: https://pm.dartus.fr/blog/a-complete-guide-on-shadow-dom-and-event-propagation/另请参阅: https://pm.dartus.fr/blog/a-complete-guide-on-shadow-dom-and-event-propagation/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何判断一个元素是否在影子 DOM 中? - How can I tell if an element is in a shadow DOM? 在阴影 DOM 元素上触发事件,该元素被 light DOM 中的元素(不是父元素)覆盖 - Trigger an event on a shadow DOM element that is overlaid by an element in the light DOM (not a parent) 如何在循环元素和使用 addEventListener 时仅更改悬停元素的样式? - How can I change only the hovered element's styling while looping through elements and using addEventListener? 如何检查元素是否可以托管 Shadow DOM? - How can i check whether an element can host Shadow DOM? 是否可以通过父文档访问 Shadow DOM 元素? - Is it possible to access Shadow DOM elements through the parent document? 如何通过“addEventListener”访问“calendarEvents”的数据元素? - How can I access the data elements of “calendarEvents” through the “addEventListener”? 我如何才能占据影子 dom javascript 中的更改事件 - how can i occupy the change event in shadow dom javascript 如何在野生动物园的Shadow DOM中找到元素? (聚合物) - How can I find element in shadow DOM in safari? (Polymer) jQuery / DOM元素“ click”事件注册:on(“ click”)VS click()VS addeventlistener(“ click”) - JQuery/DOM element “click” event registration: on(“click”) VS click() VS addeventlistener(“click”) Webdriverio:单击阴影 dom 元素 - Webdriverio: Click on a shadow dom element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM