我如何使用webcomponent.js获得样式表css规则
How can I get StyleSheet css rules using webcomponent.js
我最近正在编写一个web组件,现在尝试为其他不支持的浏览器添加webcomponentsjs多边形,但是当我使用它时我有一个问题。
我必须得到一个childNodes样式表css规则的动态变化,这里的代码在chrome工作得很好:
node = this['root'].childNodes;
//this['root']: is <template> element
//Here is webcomponentjs console.log in Firefox:
//Object
// { __impl4cf1e782hg__: <template#sd>,
// parentNode_: undefined, firstChild_: undefined,
// lastChild_: undefined, nextSibling_: undefined,
// previousSibling_: undefined, treeScope_: Object }
style = node[1]['sheet'].cssRules;
if(bgColor || hairColor){
for(i = 0; i < style.length; i++){
if(style[i].selectorText === '.base'){
style[i].style.background = bgColor;
}
if(style[i].selectorText === '.hair'
|| style[i].selectorText === '.hair::before'
|| style[i].selectorText === '.hair::after' ){
style[i].style.background = hairColor;
}
}
}
问题是我无法在firefox和safari的webcomponentsjs中访问node[1]['sheet'].cssRules
,因为它在另一层Object { __impl4cf1e782hg__: <template> ... }
中,虽然我进入了['__impl4cf1e782hg__']
,但我不允许得到任何东西。如何改变这个代码的webcomponentsjs?
问题是,我怎么能得到cssRules?
我在他们的网站上看不到任何关于获取床单的信息。有什么想法吗?
谢谢。
无论是否使用Shadow DOM,方法都是不同的。
在polyfill Shadow DOM中访问sheet
的一种方法是在attachedCallback()
方法中使用setTimeout()
函数,因为只有在那一刻之后样式表才可用。
prototype.attachedCallback = function ()
{
var style = this.shadowRoot.querySelector( 'style' )
setTimeout( function ()
{
var sheet = style.sheet
console.info( 'sheet is ok =>', sheet )
//you can modify styles here:
sheet.cssRules[0].style.color = 'dodgerblue'
} )
}
相关文章:
- 如果文本字段为空,则使用JavaScript应用CSS样式
- FF和Chromium中CSS样式按钮的外观差异
- CSS样式属性留空
- 使用javascript单击同一按钮,在两种样式之间更改css值
- 用CSS在CKEditor中设置预览样式
- Javascript+动态菜单+当前链接样式(CSS)+基于PHP的网站
- 如何使用jQuery转换所有带有动态类的内联样式css
- iFrame 中的实时预览和保存样式.css每 30 秒一次
- 如何更改 jquery UI 数据选择器的样式 (CSS)
- 在Javascript中设置左样式CSS属性以移动滑块
- css+jQuery样式.css切换器,用于新的jQuery版本
- Chrome 29样式/Css问题
- 如何更改工具提示的样式/css
- 字体Awesome与半样式CSS
- Angular2组件样式/css没有被浏览器应用在ngafterviewit中
- 如何改变网页内容,而不删除任何样式/css/图形
- 使用Javascript在不影响打印样式的情况下更改显示样式(CSS)
- 使用 Get Time Javascript 更改样式 CSS
- 自定义下拉列表样式(CSS/JS与ASP.. NET c#连接)
- 为输入类型文件按钮添加禁用样式(css)