Closed medihack closed 13 years ago
Here is the solution I came up with in Fanimate:
var rules = sheet.cssRules || sheet.rules;
for (var i = 0, length = rules.length; i < length; i++) {
if (rules[i] === rule) {
if (sheet.deleteRule) {
sheet.deleteRule(i);
}
else if (sheet.removeRule) {
sheet.removeRule(i);
}
break;
}
}
I know, it is more a sledgehammer approach, but also supported by IE 6-8 (which isn't the case for parentStyleSheet
). Fortunately I only have to loop through one style sheet in Fanimate. I guess in JSS you have too loop through any style sheet.
I hope that helps, Kai
Hi Kai,
Thanks for the feedback and the code. I'll investigate this further, but if it throws a DOMException as you say, I could use a try catch block, and resort to the looping only if an error is thrown.
Cheers.
Nope, my fault. I used deleteRule
and removeRule
incorrectly - they only accept an index as an argument in all browsers.
I've modified the _getRules
and _addRule
functions to return a custom cssrule object that exposes the sheet
, index
, and style
properties so that _removeRule
has sheet
and index
to work with, while you can still easily modify the rule using the style
object. Not sure if you want to take this approach in fanimate as well.
Thanks again for pointing out this (important) error.
Hello again David.
It seems that the jss._removeRule does not work in Opera 11 (Linux version). parentSheet.deleteRule(rule) throws an DOMException. It seems that Opera only allow to provide an index as parameter as parentSheet.deleteRule(0) works. Any suggestions how we can solve that in a nice way?