2017-08-18 4 views
0

ブラウザ:クローム60.0.3112.78なぜchromeが間違ったCSSクラスを削除するのですか?

オリジナル:

CSSRuleList {0: CSSStyleRule, 1: CSSStyleRule, 2: CSSStyleRule, 3: CSSStyleRule, length: 4} 
     length:4 
    0:CSSStyleRule {selectorText: ".highlight", style: CSSStyleDeclaration, type: 1, cssText: ".highlight { background-color: rgb(182, 246, 156); }", parentRule: null, …} 
    1:CSSStyleRule {selectorText: ".highlight:hover", style: CSSStyleDeclaration, type: 1, cssText: ".highlight:hover { background-color: rgb(124, 208, 124); }", parentRule: null, …} 
    2:CSSStyleRule {selectorText: ".highlightb6f69c:hover", style: CSSStyleDeclaration, type: 1, cssText: ".highlightb6f69c:hover { background-color: rgb(124, 208, 124); }", parentRule: null, …} 
    3:CSSStyleRule {selectorText: ".highlightb6f69c", style: CSSStyleDeclaration, type: 1, cssText: ".highlightb6f69c { background-color: rgb(182, 246, 156); }", parentRule: null, …} 

操作:

> document.getElementById('styles').sheet.removeRule('.highlightb6f69c') 

<- undefined 

> document.getElementById('styles').sheet.rules 

<- CSSRuleList {0: CSSStyleRule, 1: CSSStyleRule, 2: CSSStyleRule, 3: CSSStyleRule, length: 3} 
     length:3 
    0:CSSStyleRule {selectorText: ".highlight:hover", style: CSSStyleDeclaration, type: 1, cssText: ".highlight:hover { background-color: rgb(124, 208, 124); }", parentRule: null, …} 
    1:CSSStyleRule {selectorText: ".highlightb6f69c:hover", style: CSSStyleDeclaration, type: 1, cssText: ".highlightb6f69c:hover { background-color: rgb(124, 208, 124); }", parentRule: null, …} 
    2:CSSStyleRule {selectorText: ".highlightb6f69c", style: CSSStyleDeclaration, type: 1, cssText: ".highlightb6f69c { background-color: rgb(182, 246, 156); }", parentRule: null, …} 

私はクラス.highlightb6f69cを削除したいのですが、クロームではなく.highlightを削除し、なぜですか?

スクリーンショット enter image description here

+0

removeRule(または少なくとも私がMDNで見つけることができるものからのdeleteRule)のように見えるのは、インデックスではなく名前です。 https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/deleteRule – hank

答えて

0

https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/deleteRule

からCSSStyleSheet.deleteRule()メソッドは、現在のスタイルシートオブジェクトのスタイルルールを削除します。

構文

stylesheet.deleteRule(index) 

パラメータインデックスは、ルールの位置を表す長い数です。

ので、間違ったルールが削除される理由はの.highlightb6f69cは最初の1つを取り外し、0またはNaNとして解析されています。

関連する問題