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

问题描述

嘿,我试图弄清楚deleteRule为什么不适用于JavaScript中的CSS对象.我做了一个非常简单的测试来说明. http://codepen.io/wzsun/pen/uzcvI

Hey I'm trying to figure out why deleteRule isn't working for my css object in javascript. I made a very simple test to illustrate.http://codepen.io/wzsun/pen/uzcvI

console.log(cssRule.cssRules.length); <-- returns 2
cssRule.deleteRule(1); // <-- can't delete
cssRule.insertRule("to { -webkit-transform: rotatex(360deg); background-color:blue;}") // <-- this works
console.log(cssRule.cssRules.length); <-- returns 3
console.log(cssRule);
cssRule.deleteRule(2);

我可以插入一条规则,但不能删除一条.感谢您的帮助.

I can insert a rule but I can't delete one. Thanks for any help.

推荐答案

我对您显示的问题很感兴趣,我想我找到了答案.如果要调用removeRule,则必须按属性DOMString键删除规则

I have got intrested by the problem you shown and I think I found the answer. If you want to call removeRule you have to remove rule by attribute DOMString key

void            deleteRule(in DOMString key);

及其介于0和1之间的值(例如0%,50%,100%).那就是为什么索引传递无效的原因.

and its value between 0 and 1 (for example 0%, 50%, 100%). Thats why index passed didnt worked.

CSSKeyframesRule MDN.

您要做的是找到一个keyText( msdn mdn )和然后只需将其用于deleteRule()

What you have to do is to find a keyText ( msdn, mdn ) and then simply use it to deleteRule()

keyToRemove = cssRule.cssRules[1].keyText;
console.log(cssRule);
console.log(cssRule.cssRules.length);
cssRule.deleteRule(keyToRemove);

这是一个示例.

这篇关于Javascript CSS deleteRule属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:56