如何在不同类别的不同地方使用相同的阴影DOM
我正在使用一个预制的Web组件,称为rssapp-feed
。我想在2个不同的位置使用它
,这样的标准是原始的Shadow dom markup的样子:
和一个带修饰符的 >。
像这样:
我已经通过JavaScript添加了此修饰符类。
var a = document.querySelector('rssapp-feed')
.shadowRoot.querySelector('.rssapp-feed')
.classList.add('rssapp-feed--sidebar');
问题是我只希望仅在Web组件之一中添加修饰符类。通过添加这样的修饰符类,它一直在两个地方添加。无论如何,我适用于这两种修饰符类别的样式。就像我想设置Display:无
与类 card-root 的最后一个元素,它会影响两个组件。
有什么方法可以在两个不同类别的两个不同的位置上使用相同的Web组件?
这是我在HTML中使用的Web组件。
<rssapp-feed id="i0QRdHCkHgYPGNWs"></rssapp-feed><script src="https://widget.rss.app/v1/feed.js" type="text/javascript" async></script>
这是 codesandbox
请任何帮助都是感谢。
I'm using a pre-made web component called rssapp-feed
. I want to use this in 2 different places one standard
Like this, this is what the original shadow DOM markup looks like:
and one with modifier class rssapp-feed--sidebar
.
Like this:
I have added this modifier class through javascript.
var a = document.querySelector('rssapp-feed')
.shadowRoot.querySelector('.rssapp-feed')
.classList.add('rssapp-feed--sidebar');
The problem is I just want the modifier class to be added in one of the web components only. By adding a modifier class like this it's been adding on both the places. And by this whatever styles I give to modifier class it applies to both of those. Like if I want to set display: none
to the last element with the class card-root
it affects both the components.
Is there any way to use the same web component on two different places with different classes?
Here is the web component that I'm using in my HTML.
<rssapp-feed id="i0QRdHCkHgYPGNWs"></rssapp-feed><script src="https://widget.rss.app/v1/feed.js" type="text/javascript" async></script>
Here is the example on codeSandbox
Please any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所需要的只是一个更好的CSS选择器,可以传递到
QuerySelector
函数。如果您控制了&lt; rssapp-feed&gt;/rssapp-feed&gt;
的代码,则只需添加一些唯一的class
或data- *
将其归因于此。然后使用它查询。使用类:
在您的JS代码中:
或使用
数据 - *
属性:在JS代码中:
最后,如果您没有对代码的控制Web组件独有的。
All you need is a better CSS selector to pass to the
querySelector
function. If you have control over the code where<rssapp-feed></rssapp-feed>
is being added then simply add some uniqueclass
ordata-*
attribute to it. And then query using it.Using class:
In your JS code:
Or using
data-*
attribute:In your JS code:
Finally, if you don't have control over the code, use some nested CSS selector using some parent which is unique to the web-component.
您可以扩展任何定义的 Web组件; 在其顶部构建自己的 Web组件:
示例:您可以使用 this dev.to post
You can extend any defined Web Component; and build your own Web Component on top of it:
Example: You can steal a 52 SVG playing cards Web Component with this Dev.to Post