How to target a class in shadow root without JS?
P粉610028841
P粉610028841 2024-03-30 12:47:21
0
2
341

Suppose I have a html element with a shadow root.

<my-element>
#shadow-root
<div class='need-target-this' />
</my-element>

How to position the div inside the Shadow root?

I tried using

:host(my-element.need-target-this)

But that doesn't help. What am I missing here?

P粉610028841
P粉610028841

reply all(2)
P粉521013123

customElements.define("my-element",class extends HTMLElement{
  constructor(){
    super().attachShadow({mode:"open"}).innerHTML = `
    
    
    `;
  }
  connectedCallback(){
    this.shadowRoot.querySelector("span").innerHTML = `Web Component!`;
  }
});


Hello

P粉232793765

In case it helps someone: I wrapped my element with div, added ref and went to

const Shadow = ref.current.querySelector('my-element').shadowRoot

const target = Shadow?.querySelector('.need-target-this')

target.style.whatever = 'value';

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!