insertRule
を使用してCSSOMでスタイルを追加した場合、2つのことに気付きました。CSSOMのinsertRuleを使用した後、スタイルが見えなくなり、スタイルタグが再追加できない
追加されたスタイルはFirebugで表示中にhtmlに表示されません。
スタイルタグが別の要素に追加(例:頭から体に移動)されていると、追加されたスタイルは機能しません(FirefoxではHappens、ChromeではHappens)。
タグが付加された後にスタイルが追加された場合、それらは機能します。彼らはまだFirebugには表示されません。シートを追加し直す必要があります。
Firebugには表示されないため、Firebugでは気まぐれかもしれませんが、動的に追加されない通常のスタイルが表示されます。
追加の後に動作しないスタイルについては、FirefoxとChromeでこれが発生するため、これが標準であるかどうか疑問に思っています。基準を見ると、私はこれについて何も見ませんでした。私がそれらを理解していない限り。明確にするため
var style = document.createElement('style'),
sheet = style.sheet;
document.head.appendChild(style);
//When line 6 is un-commented the styles work
//if line 8 is also commented out.
//document.querySelector('.container').appendChild(style);
//Get the sheet when line 6 is un-commented
sheet = style.sheet
sheet.insertRule('.test{color: red}');
document.querySelector('.container').appendChild(style);
//styles don't apply after the previous line
編集:あなたはstyle.sheet.insertRule(styleString)
でのスタイルを適用することができ、HTMLの<head></head>
から<style></style>
タグを追加し、スタイルがドキュメントに適用される追加した場合は
。
すでに<head><style></style></head>
よう<head></head>
から<style></style>
、とは<div><style></style></div>
すべてのスタイルが失われているようにどこか<style></style>
を追加しようとすると、再度適用されないことを追加した場合。
これは正常ですか?
コードフロー:
作品:
- 動作しません
style.sheet.insertRule(styleString)
<style>
任意の
でのスタイルを追加
- AP
<style>
- に
style.sheet.insertRule(styleString)
でのスタイルを追加した保留<style>
は
私の他の問題は、スタイルはそれらが適用されている場合でもFirebug
に表示されません<style></style>
に追加点である同じ<style>
どこか別の場所に追加します資料。
編集がより透明度:
私はスタイルシートを変更せずにstyle
要素をreappend場合は、スタイルが残っている:
var style = document.querySelector('style');
document.head.appendChild(style);
* {color: red}
<p>Hello world</p>
しかし、私はスタイルシートを変更した場合JSでは、変更は取り消されます:
var style = document.querySelector('style'),
sheet = style.sheet;
sheet.insertRule('* {color: red}', 0);
document.head.appendChild(style); //Comment this line out, and this works.
/* CSS rules will be inserted with JS */
<p>Hello world</p>
されていない何の質問特定の? – guest271314
主に、既にスタイルがinsertRule normalで追加されているスタイルタグを追加した後にスタイルが適用されないという事実はありますか? –
Questionを正しく解釈すると、これは 'div {color:green}; div {color:blue}に似ています。' 'blue''は最後の宣言である' div'に 'color'を設定します。 http://stackoverflow.com/questions/1043001/what-is-the-meaning-of-cascading-in-css – guest271314